Ex No: 11 Demonstrate the Database - Delete Operation
Aim: To write a PHP script to demonstrate database delete operation.
Algorithm
Step1: Use the stud_db database with stud_table.
Step2: Get the student roll number of the student to be deleted from the databse.
Step3: CHeck if roll number is present for deletion.
   A) Include the database class file.
   B) Create a new database object and get student roll number for deletion.
   C) Call the delete function using object to delete the data in the database.
   D) After deletion display success message.
db_class.php
<?php
 class Database
 {
  var $hostadd;
  var $usr;
  var $pwd;
  var $DB;
  var $db_con;
  
  public function __construct()
  {
   $this->hostadd = "127.0.0.1";
   $this->usr = "root";
   $this->pwd = "";
   $this->DB= "stud_db";
   $this->db_con = mysqli_connect($this->hostadd,$this->usr,$this->pwd,$this->DB);
   
   if(mysqli_connect_errno())
   {
   	header("Location:error.php");
   	return mysqli_connect_errno();
   	exit();
   }
   else
   {
   	return 1;
   } 
  }
  
  public function del_stud($stud_roll)
  {
   $qry = "delete from stud_table where roll_no = $stud_roll";
   $res = mysqli_query($this->db_con,$qry) or die("error in query");
   $numrows = mysqli_affected_rows($this->db_con);
   return $numrows;
  }	
  
  public function __destruct()
  {
   mysqli_close($this->db_con);
  }
 }
?>
            
ex11.php
            
  
<html>
<head>
 <meta charset="utf-8">
 <title>Student Management System</title> 
 </head>
 <body> 
 <?php
  $style = $res = "";  
  $delstyle ="display:none;";
  if($_SERVER["REQUEST_METHOD"] == "POST")
  {
   include("db_class.php");
   $db_obj = new Database();
   if(isset($_POST["roll_no"]))
   {
   	$roll_no = $_POST["roll_no"];
   	$res = $db_obj->del_stud($roll_no);
   	if($res>=1)
    {
     $style = "display: none;";
   	 $delstyle="";
    }
    else
    {
   ?>
   <script>
   	alert("Roll Number Not Found");
   </script>
 <?php	}
   }      
   unset($db_obj);
  }

 ?>
 
 <div style="<?php echo $delstyle; ?>">
  <h1>Student Data Deleted</h1>
 </div> 
 
 <div style="<?php echo $style;?>">   
 <h1><u>Delete Student Data</u></h1>
 <form method="POST" name="search" action="<?php echo $_SERVER["PHP_SELF"];?>"> 
 <table>
  <tr>
   <td>Roll No:</td>
   <td><input type="text" required="true" name="roll_no"/></td>
  </tr>
  <tr>
   <td><input type="submit" value="Delete"/></td>
   <td><input type="reset"/></td>
  </tr>
 </table>  
 </form>
 </div>  
 </body>
</html>
            
Result: Thus a database delete operation is demonstrated using a webpage with php script.