PHP Examples - Student Details - Delete Data From Database - Form

PHP Examples - Student Details
Delete Data From Database -  Form

Program:

// php10.php

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP EXAMPLE 9 : Database with Form - Deletion </title>
</head>

<body>

<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Delete Data From DATABASE";
 echo"</h1>";
 echo"<br/>";
 
 // echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
  die('Connection Failed'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  
  mysql_select_db("DB1",$con);  // Creating tables
  
  mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
 }
 $data= mysql_query(" SELECT Rollnum FROM Students ",$con); // Retreiving data
 echo"<center>
 <h2><b><u> STUDENT DETAILS </u></b></h2>
 <form method=\"post\" action=\"deletedata.php\"><br/><br/><br/>
 Select Roll Number: <select name=\"selection\">";

 while($rec=mysql_fetch_array($data))
 {
  echo "<option>".$rec['Rollnum']."</option>";  
 }
 
 echo "</select>
 <input type=\"submit\" value=\"Delete\" />
 </form>
 </center>";
 mysql_close($con);  // Closing connection
?>
</body>
</html>

// deletedata.php

<html >
<head>

<title>PHP EXAMPLE 9 : Database with Form - Deletion</title>
</head>

<body>
<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Delete Data From DATABASE";
 echo"</h1>";
 echo"<br/>";
 
 //echo $_POST['selection'];
 
 echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
   die('Connection Failed'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  mysql_select_db("DB1",$con);  // Creating tables
  mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
 }
 $deleteqry="SELECT * FROM Students where Rollnum =". $_POST['selection'];
 $data1= mysql_query($deleteqry,$con); // Retreiving data
  
  //echo $data1;
  if($rec=mysql_fetch_array($data1))
   echo "<br/>Deleting...<br/>".$rec['Rollnum']." ".$rec['Name']." ".$rec['Mark']."<br/>";  
  else
   die( "<br/> Error in database access...".mysql_errno());  
  
  $deleterec = "DELETE FROM Students WHERE Rollnum=".$_POST['selection'];
 if(!mysql_query($deleterec,$con))
   die( "<br/> Error in deletion...".mysql_errno());  
 echo"<br><br>Record Deleted...<br/> Updated Table...";    

$data= mysql_query(" SELECT * FROM Students ",$con); // Retreiving data

 echo "<table border='2'>
   <tr> 
   <th> Roll No</th>
   <th> Name</th>
   <th> Mark </th>
   </tr>";

 while($rec=mysql_fetch_array($data))
 {
  echo "<tr><td>".$rec['Rollnum']."</td><td>".$rec['Name']."</td><td>".$rec['Mark']."</td></tr>";  
 }
 echo"</table>";
 
 // mysql_query(" DROP DB1 ",$con);
 mysql_close($con);  // Closing connection

 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

Output:



6 comments:

Related Posts Plugin for WordPress, Blogger...