PHP - Remove Multiple Row using MySQLi

In this tutorial we will create Remove Multiple Row using MySQLi using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

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 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_row, 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_row');
  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 navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Remove Multiple Row using MySQLi</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="save.php">
  18. <div class="form-inline">
  19. <label>Firstname</label>
  20. <input type="text" name="firstname" class="form-control"/>
  21. </div>
  22. <br />
  23. <div class="form-inline">
  24. <label>Lastname</label>
  25. <input type="text" name="lastname" class="form-control"/>
  26. </div>
  27. <br />
  28. <div class="form-inline">
  29. <label>Gender</label>
  30. <input type="text" name="gender" class="form-control"/>
  31. </div>
  32. <br />
  33. <div class="form-inline">
  34. <label>Address</label>
  35. <input type="text" name="Address" class="form-control"/>
  36. </div>
  37. <br />
  38. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Add</button>
  39. </form>
  40. <br />
  41. <table class="table table-bordered">
  42. <form method="POST" action="remove.php">
  43. <thead class="alert-info">
  44. <tr>
  45. <th>#</th>
  46. <th>Firstname</th>
  47. <th>Lastname</th>
  48. <th>Gender</th>
  49. <th>Address</th>
  50. </tr>
  51. </thead>
  52. <tbody>
  53. <?php
  54. require 'conn.php';
  55.  
  56. $query = mysqli_query($conn, "SELECT * FROM `member` ORDER BY `lastname` ASC") or die(mysqli_error());
  57. while($fetch = mysqli_fetch_array($query)){
  58. ?>
  59. <tr>
  60. <td><input type="checkbox" name="mem_id[]" value="<?php echo $fetch['mem_id']?>"></td>
  61. <td><?php echo $fetch['firstname']?></td>
  62. <td><?php echo $fetch['lastname']?></td>
  63. <td><?php echo $fetch['gender']?></td>
  64. <td><?php echo $fetch['address']?></td>
  65. </tr>
  66. <?php
  67. }
  68. ?>
  69. </tbody>
  70. <tfooter>
  71. <tr>
  72. <td></td>
  73. <td></td>
  74. <td></td>
  75. <td></td>
  76. <td><button class="btn btn-danger" name="remove">Remove</button></td>
  77. </tr>
  78. </tfooter>
  79. </form>
  80. </table>
  81. </div>
  82. </body>
  83. </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.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $gender = $_POST['gender'];
  8. $address = $_POST['address'];
  9.  
  10. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$gender', '$address')") or die(mysqli_error());
  11.  
  12. header('location: index.php');
  13. }
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will remove a multiple row of data in an instance. To make this just follow and write the below inside the text editor, then save it as remove.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['remove'])){
  5. if(ISSET($_POST['mem_id'])){
  6. foreach($_POST['mem_id'] as $key => $value){
  7. mysqli_query($conn, "DELETE FROM `member` WHERE `mem_id` = '$value'") or die(mysqli_error());
  8. }
  9.  
  10. header('location: index.php');
  11. }else{
  12. echo "<script>alert('Please select something first!')</script>";
  13. echo "<script>window.location= 'index.php'</script>";
  14. }
  15. }
  16. ?>
There you have it we successfully created 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