Delete Multiple Selected Records with PHP

It is time-consuming to delete records one by one from the list.

To make the deletion process easier for the users by allowing them to select multiple records using checkboxes.

Execute delete query according to the selected checkboxes.

In the demonstration, I am displaying records from the MySQL database table and add a checkbox on each row. Use single submit button to delete selected records with PHP.

Delete Multiple Selected Records with PHP


Contents

  1. Table structure
  2. Configuration
  3. Record List
  4. Delete
  5. Conclusion

1. Table structure

Create users table.

CREATE TABLE `users` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `name` varchar(60) NOT NULL,
  `gender` varchar(10) NOT NULL,
  `email` varchar(60) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Configuration

Create a config.php for database connection.

Completed Code

<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
   die("Connection failed: " . mysqli_connect_error());
}

3. Record List

Create a submit button in <form> and list users table records in <table>. On each <tr> add a checkbox element on end cell.

Set checkbox element name as delete[] and use userid in the value attribute.

Completed Code

<?php include "config.php"; ?>
<div class='container'>

  <!-- Form -->
  <form method='post' action=''>
    <input type='submit' value='Delete' name='but_delete'><br><br>

    <!-- Record list -->
    <table border='1' id='recordsTable' style='border-collapse: collapse;' >
      <tr style='background: whitesmoke;'>
        <th>Username</th>
        <th>Name</th>
        <th>Gender</th>
        <th>Email</th>
        <th>&nbsp;</th>
     </tr>

     <?php 
     $query = "SELECT * FROM users";
     $result = mysqli_query($con,$query);
     
     while($row = mysqli_fetch_array($result) ){
        $id = $row['id'];
        $username = $row['username'];
        $name = $row['name'];
        $gender = $row['gender'];
        $email = $row['email'];
     ?>
     <tr id='tr_<?= $id ?>'>

        <td><?= $username ?></td>
        <td><?= $name ?></td>
        <td><?= $gender ?></td>
        <td><?= $email ?></td>

        <!-- Checkbox -->
        <td><input type='checkbox' name='delete[]' value='<?= $id ?>' ></td>
 
    </tr>
    <?php
    }
    ?>
   </table>
 </form>
</div>

4. Delete

On Delete button click read checked checkboxes by looping on $_POST['delete'] and get the delete ids.

Execute Delete query on id.

Completed Code

<?php 
if(isset($_POST['but_delete'])){

  if(isset($_POST['delete'])){
    foreach($_POST['delete'] as $deleteid){

      $deleteUser = "DELETE from users WHERE id=".$deleteid;
      mysqli_query($con,$deleteUser);
    }
  }
 
}
?>

5. Conclusion

Use checkbox element to allow the users for multiple record selection. On form submit read the checked checkboxes values and execute the Delete query.

You can also add check uncheck multiple checkboxes with jQuery or AngularJS.

If you found this tutorial helpful then don't forget to share.

2 thoughts on “Delete Multiple Selected Records with PHP”

  1. Great help I was able to use this!…but however when I used
    $(document).ready(function(){
    $(“#mtab”).DataTable({
    });
    });

    for displaying enabling search field, the checkbox disappeared from the table???

    Reply

Leave a Comment