How to Delete multiple Rows or records from a mysql table using php

How to Delete multiple Rows or records from a mysql table using php

If you want to delete multiple record from any MySQL table,
then you can use SQL command DELETE FROM. You can use this command in any scripting language like PHP.
but you can’t delete multiple recoreds from mysql table without use any loop.
you have need of php loop to delete multiple records from mysql table.

Here we are explaining some examples for you that examples my be help of you to solve your problem,
in these example we are deleting data using different-2 examples

Example 1.
this is the simple example in this we are using a simple while loop…using while loop we trying to delete multiple records from the mysql table.

 <?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'dbpassword';
$conn = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db('TUTORIALS') or die(mysql_error());


$count=1;
while($count<=10)
{
$sql = 'DELETE FROM tbl_student WHERE student_id=$count';


$retval = mysql_query($sql);
if(! $retval )
{
  die('Could not delete data: ' . mysql_error());
}
$count++;
}
echo "Deleted data successfully from student table\n";

?>

Example 2.
In this we are using a Check boxs for deleting rows from mysql table.
here we are first using mysql select query for select multiple records from mysql table with those records we are
using html check box, using check box we will be select rows which we want to delete and end of records we are taking
a submit button for performing delete operation on selected rows.

Example of delete multiple records from mysql table.

<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'dbpassword';
$conn = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db('TUTORIALS') or die(mysql_error());
?>

<?php
if(isset($_POST['delete']))
{
    $scheck=$_POST['inbcheck'];
    foreach($scheck as $v)
    {
        mysql_query("delete from tbl_student where id='$v'");
        
        echo "Records delete SuccessFuly From Table Student";
    }
}
?>
<html>
<head>
    <title>Delete selected records multiple data from mysql table using php and mysql</title>
</head>
<body>
     <form method="post">
     
     <table>
         <tr>
            <th>S. No.</th>
            <th>Student Name</th>
            <th>Student Roll No.</th>
            <th>Student Class</th>
        </tr>
      <?php
      $count=1;
      $res=mysql_query("select * from tbl_student");
      while($resfet=mysql_fetch_assoc($res))
      {
        ?>
    <tr>
        <td><input type="checkbox" name="inbcheck[]" value="<?php echo $resfet['id'];?>" /><?php echo $count;?></td>
        <td><?php echo $resfet['student_name'];?></td>
        <td><?php echo $resfet['student_rool_no'];?></td>
        <td><?php echo $resfet['student_class'];?></td>
   </tr>    
    <?php $count++; }
      ?>
      </table>
      <input class="btn" type="submit" name="delete" value="   Delete Selected Records   ">
    </form>
</body>
</html>





About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.