PHP - Delete Multiple Row Using MySQLi

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

In this tutorial we will create a Delete Multiple Row using PHP. This code can delete a multiple of data when the user click the delete button.The code use MySQLi PHP POST to call a function that delete a data that have been checked via javascipt code in order to use the DELETE query that delete a data in the database table by providing a given id in the WHERE clause. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_multiple_delete, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_multiple_delete");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Delete Multiple Row Using MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button class="btn btn-primary" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add User</button>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <form method="POST" action="delete.php">
  22. <thead class="alert-info">
  23. <tr>
  24. <th><input type="checkbox" onclick="selectedCheckbox(this)"> Select All</th>
  25. <th>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Address</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33. $query=mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  34. $count_query=mysqli_query($conn, "SELECT COUNT(*) as count FROM `user` ORDER BY `lastname` ASC") or die(mysqli_error());
  35. $row=mysqli_fetch_array($count_query);
  36. $count=$row['count'];
  37. while($fetch=mysqli_fetch_array($query)){
  38. ?>
  39. <tr>
  40. <?php
  41. if($count != 0){
  42. ?>
  43. <td><input type="checkbox" name="check[]" onclick="countCheckbox()" value="<?php echo $fetch['user_id']?>"></td>
  44. <?php
  45. }
  46. ?>
  47. <td><?php echo $fetch['firstname']?></td>
  48. <td><?php echo $fetch['lastname']?></td>
  49. <td><?php echo $fetch['address']?></td>
  50. </tr>
  51. <?php
  52. }
  53. ?>
  54. </tbody>
  55. <?php
  56. if($count != 0){
  57. ?>
  58. <tfoot>
  59. <tr>
  60. <td colspan="5"><button name="delete" class="btn btn-danger pull-right"><span id="count" class="badge">0</span> Delete</button></td>
  61. </tr>
  62. </tfoot>
  63. <?php
  64. }
  65. ?>
  66. </form>
  67. </table>
  68. </div>
  69. <div class="modal fade" id="form_modal" aria-hidden="true">
  70. <div class="modal-dialog">
  71. <div class="modal-content">
  72. <form method="POST" action="save_user.php">
  73. <div class="modal-header">
  74. <h3 class="modal-title">Add User</h3>
  75. </div>
  76. <div class="modal-body">
  77. <div class="col-md-2"></div>
  78. <div class="col-md-8">
  79. <div class="form-group">
  80. <label>Firstname</label>
  81. <input type="text" name="firstname" class="form-control" required="required"/>
  82. </div>
  83. <div class="form-group">
  84. <label>Lastname</label>
  85. <input type="text" name="lastname" class="form-control" required="required"/>
  86. </div>
  87. <div class="form-group">
  88. <label>Address</label>
  89. <input type="text" name="address" class="form-control" required="required"/>
  90. </div>
  91. </div>
  92. </div>
  93. <div style="clear:both;"></div>
  94. <div class="modal-footer">
  95. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  96. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  97. </div>
  98. </form>
  99. </div>
  100. </div>
  101. </div>
  102. <script src="js/jquery-3.2.1.min.js"></script>
  103. <script src="js/bootstrap.js"></script>
  104. <script src="js/script.js"></script>
  105. </body>
  106. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_user.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $address = $_POST['address'];
  8.  
  9. mysqli_query($conn,"INSERT INTO `user` VALUES('', '$firstname', '$lastname', '$address')");
  10.  
  11. header('location: index.php');
  12. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will delete multiple row table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
delete.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['delete'])){
  5. if(ISSET($_POST['check'])){
  6. $checked = $_POST['check'];
  7. for($i=0; $i < count($checked); $i++){
  8. $id = $checked[$i];
  9. mysqli_query($conn, "DELETE FROM `user` WHERE `user_id`='$id'") or die(mysqli_error());
  10. }
  11.  
  12. header('location:index.php');
  13. }else{
  14. echo "<script>alert('Please select an item first!')</script>";
  15. echo "<script>window.location='index.php'</script>";
  16. }
  17. }
  18. ?>

script.js
Note: To make this script work make sure you save this file inside the js directory.

  1. function selectedCheckbox(check){
  2. var checkboxes = document.getElementsByName('check[]');
  3. for(var i in checkboxes){
  4. checkboxes[i].checked = check.checked;
  5. }
  6.  
  7. var count = document.querySelectorAll('input[name="check[]"]:checked').length;
  8. document.getElementById('count').innerHTML = count;
  9. }
  10.  
  11. function countCheckbox(){
  12. var count = document.querySelectorAll('input[name="check[]"]:checked').length;
  13. document.getElementById('count').innerHTML = count;
  14. }

There you have it we successfully created Delete Multiple Row using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


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.