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>





Leave a Reply

Related Posts: You may like

How to remove whitespace from string using php

Remove All Special Characters From String PHP

comma separated values in mysql query

SQL statement to retrieve names beginning with A

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.