Delete multiple records from MySQL in PHP with checkbox- Tutorialpath

Welcome to all our readers on our website at As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which help you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending part in this world. So, here again we came up with a latest and new tech information which is related to the process of creating Delete multiple records from MySQL in PHP with checkbox. With the help of this article you will get to know the complete process of how to create Delete multiple records from MySQL in PHP with checkbox in a step by step manner. So, simply have a look to this article and grab all the useful information which is going to be very helpful for you.

As we know, it takes a lot of time to destroy records one by one. Everyone wants to delete bulk records on only one event.

So here I will tell you how to delete bulk records from MySQL table in PHP with checkbox, all you have to do is select the checkbox you want to delete and hit the submit button to delete.

It is strongly recommended for large-scale data, where it is not possible to delete it one by one.

In this tutorial, I will create a product table for the demo with a dummy record to test this code. And every row will be accompanied by a checkbox that will hold the ID of the record in the array. There will also be a checkbox with a header to select all records on a single click. When you are going to delete the records, you will be asked that “Do you really want to delete the records” when you click Yes, the selected records will be deleted from the database table.

Products table

In first step, create a products table in database and insert some dummy records after creating a table.

  1. CREATE TABLE `products` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `details` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `created_at` datetime NOT NULL,
  6. `updated_at` datetime NOT NULL,
  7. `status` tinyint(1) NOT NULL DEFAULT ‘1’ COMMENT ‘1=Active, 0=Deactive’,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Now i have a products table with idnamedetailscreated_atupdated_Atstatus fields.

Database Configuration : db_config.php

Now i need to setup my database configuration in db_config.php file.

  1. <?php
  2. $dbHost = ‘localhost’; //database host name
  3. $dbUser = ‘root’; //database username
  4. $dbPass = ; //database password
  5. $dbName = ‘demo’; //database name
  6. $conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
  7. if(!$conn){
  8. die(“Database connection failed: “ . mysqli_connect_error());
  9. }
  10. ?>


In this step, create a file where we list all records from the database into the html table but first include db_config.php for database access.

To work with js script, include the jQuery library.

The deleteConfirm () method is a JavaScript function called to submit the form to ensure that you actually want to delete the product.

  1. <!DOCTYPE html>
  2. <html lang=“en-US”>
  3. <head>
  4.     <title>Delete multiple records from MySQL in PHP with checkbox – ExpertPHP</title>
  5. <link rel=“stylesheet” href=“”>
  6.     <script src=“”></script>
  7. </head>
  8. <body>
  9. <?php
  10.     session_start();
  11. include_once(‘db_config.php’);
  12. $query = mysqli_query($conn,”SELECT * FROM products”);
  13. ?>
  15. <form name=“actionForm” action=“action.php” method=“post” onsubmit=“return deleteConfirm();”/>
  16. <table class=“table-striped”>
  17. <thead>
  18. <tr>
  19. <th><input type=“checkbox” name=“check_all” id=“check_all” value=“”/></th>
  20. <th>Name</th>
  21. <th>Details</th>
  22. </tr>
  23. </thead>
  24. <?php
  25. if(mysqli_num_rows($query) > 0){
  26. while($row = mysqli_fetch_assoc($query)){
  27.     extract($row);
  28. ?>
  29. <tr>
  30. <td align=“center”><input type=“checkbox” name=“selected_id[]” class=“checkbox” value=“<?php echo $id; ?>”/></td>
  31. <td><?php echo $name; ?></td>
  32. <td><?php echo $details; ?></td>
  33. </tr>
  34. <?php } }else{ ?>
  35. <tr><td colspan=“3”>No records found.</td></tr>
  36. <?php } ?>
  37. </table>
  38. <input type=“submit” class=“btn btn-primary” name=“btn_delete” value=“Delete”/>
  39. </form>
  40. <script type=“text/javascript”>
  41. function deleteConfirm(){
  42. var result = confirm(“Do you really want to delete records?”);
  43. if(result){
  44. return true;
  45. }else{
  46. return false;
  47. }
  48. }
  49. $(document).ready(function(){
  50. $(‘#check_all’).on(‘click’,function(){
  51. if(this.checked){
  52. $(‘.checkbox’).each(function(){
  53. this.checked = true;
  54. });
  55. }else{
  56. $(‘.checkbox’).each(function(){
  57. this.checked = false;
  58. });
  59. }
  60. });
  61. $(‘.checkbox’).on(‘click’,function(){
  62. if($(‘.checkbox:checked’).length == $(‘.checkbox’).length){
  63. $(‘#check_all’).prop(‘checked’,true);
  64. }else{
  65. $(‘#check_all’).prop(‘checked’,false);
  66. }
  67. });
  68. });
  69. </script>
  70. </body>
  71. </html>


In this step create a php file where we handle form data. When we click on submit button the form will be submitted on their action page.

In this PHP file we will find all the selected ID of records which will be removed from the product table.

Once the records are deleted we store the message in the session according to the response to inform the user.

  1. <?php
  2. session_start();
  3. include_once(‘db_config.php’);
  4. if (count($_POST[“selected_id”]) > 0 ) {
  5.      $all = implode(“,”, $_POST[“selected_id”]);
  6.      $query=“DELETE FROM products WHERE 1 AND id IN($all)”;
  7.      if(mysqli_query($conn,$query)){
  8.          $_SESSION[‘success’] = ‘Products have been deleted successfully.’;
  9.      }
  10.     }else{
  11.         $_SESSION[‘error’] = ‘Select checkbox to delete product.’;
  12.     }
  13. header(“Location:index.php”);
  14. ?>


So, hopefully we had shared all the basic steps which help you to Delete multiple records from MySQL in PHP with checkboxIf you are using any of the above given browser and the steps helps you to enable or disable the JavaScript then simply share this article with others and also tell us by dropping a comment in the comment section below so that we get to know how helpful this article is for you all.

Leave a Reply

Your email address will not be published. Required fields are marked *