How to Make Delete Multiple Rows Using PHP

Submitted by: 
Language: 
Visitors have accessed this post 4641 times.

Creating a website which and add an ability to delete multiple rows at a time?
This tutorial is a perfect outlet for you to learn and implement the function. we will look on how to delete multiple values from a database using PHP. In order to do so we will be using MySQL along with PHP. You can find more about object oriented approach to CRUD Operations.

Overview

The tutorial requires to create following PHP files. You need to copy the codes in respective files and save all the files in same folder with .php extension. The functions of each of the files will be discussed in their respective part below.

  • Action.php
  • Dbconfig.php
  • Index.php
  • Style.css

Database Schema

  1. CREATE TABLE `users` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `first_name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
  4. `last_name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
  5. `email` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
  6. `phone` VARCHAR(15) COLLATE utf8_unicode_ci NOT NULL,
  7. `created` datetime NOT NULL,
  8. `modified` datetime NOT NULL,
  9. `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active, 0=Deactive',
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  12.  
  13. INSERT INTO `users` (`id`, `first_name`, `last_name`, `email`, `phone`, `created`, `modified`, `status`) VALUES
  14. (NULL, 'Nitya', 'Maity', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  15. (NULL, 'Codex', 'World', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  16. (NULL, 'Raj', 'Ans', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  17. (NULL, 'John', 'Thomas', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1),
  18. (NULL, 'Kate', 'Bell', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '123456', '2015-04-17 00:00:00', '2015-04-17 00:00:00', 1);

Action.php

In this file, we have the code which takes the array of ID’s that is needed to be deleted. Inside a loop, it will iterate over the values and issue SQL queries to delete the entries.

  1. <?php
  2. include_once('dbConfig.php');
  3. if(isset($_POST['bulk_delete_submit'])){
  4. $idArr = $_POST['checked_id'];
  5. foreach($idArr as $id){
  6. mysqli_query($conn,"DELETE FROM users WHERE id=".$id);
  7. }
  8. $_SESSION['success_msg'] = 'Delete Successful';
  9. header("Location:index.php");
  10. }
  11. ?>

Dbconfig.php

This file contains the database configuration codes needed to establish the connection with database.

  1. <?php
  2. $dbHost = 'localhost';
  3. $dbUser = 'root';
  4. $dbPass = '';
  5. $dbName = 'muldeldb';
  6. $conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
  7. if(!$conn){
  8. die("Database connection failed: " . mysqli_connect_error());
  9. }
  10. ?>

Index.php

The codes in this file, renders the table which shows all the user data. Here, every row will contain a check box. When the delete button is pressed all the selected data will be deleted in the same time. To check all the selected check boxes a JQuery function has been used which we already discussed in the previous tutorial.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Tutorial-27</title>
  5. <link rel="stylesheet" href="style.css"/>
  6. <script type="text/javascript" src="jquery.min.js"></script>
  7. <script type="text/javascript">
  8. function delete_confirm(){
  9. var result = confirm("Are you sure to delete users?");
  10. if(result){
  11. return true;
  12. }else{
  13. return false;
  14. }
  15. }
  16.  
  17. $(document).ready(function(){
  18. $('#select_all').on('click',function(){
  19. if(this.checked){
  20. $('.checkbox').each(function(){
  21. this.checked = true;
  22. });
  23. }else{
  24. $('.checkbox').each(function(){
  25. this.checked = false;
  26. });
  27. }
  28. });
  29.  
  30. $('.checkbox').on('click',function(){
  31. if($('.checkbox:checked').length == $('.checkbox').length){
  32. $('#select_all').prop('checked',true);
  33. }else{
  34. $('#select_all').prop('checked',false);
  35. }
  36. });
  37. });
  38. </script>
  39. </head>
  40.  
  41. <body>
  42. <?php session_start();
  43. if(!empty($_SESSION['success_msg'])) {
  44. ?>
  45.  
  46. <div class="alert alert-success"><?php echo $_SESSION['success_msg'];
  47. ?>
  48. </div>
  49. <?php unset($_SESSION['success_msg']); } ?>
  50. <?php
  51. include_once('dbConfig.php');
  52. $query = mysqli_query($conn,"SELECT * FROM users");
  53. ?>
  54. <form name="bulk_action_form" action="action.php" method="post" onSubmit="return delete_confirm();"/>
  55. <table class="bordered">
  56. <thead>
  57. <tr>
  58. <th><input type="checkbox" name="select_all" id="select_all" value=""/></th>
  59. <th>First Name</th>
  60. <th>Last Name</th>
  61. <th>Email</th>
  62. <th>Phone</th>
  63. </tr>
  64. </thead>
  65. <?php
  66. if(mysqli_num_rows($query) > 0){
  67. while($row = mysqli_fetch_assoc($query)){
  68. ?>
  69. <tr>
  70. <td align="center"><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['id']; ?>"/></td>
  71. <td><?php echo $row['first_name']; ?></td>
  72. <td><?php echo $row['last_name']; ?></td>
  73. <td><?php echo $row['email']; ?></td>
  74. <td><?php echo $row['phone']; ?></td>
  75. </tr>
  76. <?php } }else{ ?>
  77. <tr><td colspan="5">No records found.</td></tr>
  78. <?php } ?>
  79. </table>
  80. <input type="submit" class="btn btn-danger" name="bulk_delete_submit" value="Delete"/>
  81. </form>
  82. </body>
  83.  
  84. </html>

Style.css

The CSS codes in the file will add some design to the form or display.

  1. body {
  2. width: 600px;
  3. margin: 40px auto;
  4. font-family: 'trebuchet MS', 'Lucida sans', Arial;
  5. font-size: 14px;
  6. color: #444;
  7. }
  8.  
  9. h1{
  10. font-weight: normal;
  11. color: #024457;
  12. }
  13.  
  14. h1 span{color: #167F92;}
  15.  
  16. table {
  17. *border-collapse: collapse; /* IE7 and lower */
  18. border-spacing: 0;
  19. width: 100%;
  20. }
  21.  
  22. .bordered {
  23. border: solid #ccc 1px;
  24.  
  25. -webkit-box-shadow: 0 1px 1px #ccc;
  26. -moz-box-shadow: 0 1px 1px #ccc;
  27. box-shadow: 0 1px 1px #ccc;
  28. }
  29.  
  30. .bordered td, .bordered th {
  31. border-left: 1px solid #ccc;
  32. border-top: 1px solid #ccc;
  33. padding: 10px;
  34. text-align: left;
  35. }
  36.  
  37. .bordered th {
  38. background-color: #dce9f9;
  39. background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
  40. background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
  41. background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
  42. background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
  43. background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
  44. background-image: linear-gradient(top, #ebf3fc, #dce9f9);
  45. -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  46. -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
  47. box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
  48. border-top: none;
  49. text-shadow: 0 1px 0 rgba(255,255,255,.5);
  50. }
  51.  
  52. .bordered td:first-child, .bordered th:first-child {
  53. border-left: none;
  54. }
  55.  
  56. .btn {
  57. display: inline-block;
  58. padding: 6px 12px;
  59. margin-bottom: 0;
  60. margin-top: 10px;
  61. font-size: 14px;
  62. font-weight: 400;
  63. line-height: 1.42857143;
  64. text-align: center;
  65. white-space: nowrap;
  66. vertical-align: middle;
  67. -ms-touch-action: manipulation;
  68. touch-action: manipulation;
  69. cursor: pointer;
  70. -webkit-user-select: none;
  71. -moz-user-select: none;
  72. -ms-user-select: none;
  73. user-select: none;
  74. background-image: none;
  75. border: 1px solid transparent;
  76. border-radius: 4px;
  77. }
  78. .btn-danger {
  79. color: #fff;
  80. background-color: #d9534f;
  81. border-color: #d43f3a;
  82. }
  83. .alert {
  84. padding: 15px;
  85. margin-bottom: 20px;
  86. border: 1px solid transparent;
  87. border-radius: 4px;
  88. }
  89. .alert-success {
  90. color: #3c763d;
  91. background-color: #dff0d8;
  92. border-color: #d6e9c6;
  93. }

Tags: 

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.