Deleting Multiple Rows using PHP

In this tutorial, we are going to create Deleting Multiple Rows using PHP. The features of this project are to add a data then, you can delete individual data of a user or you can delete multiple data using a checkbox as our selector. Hope you find this useful.

Creating Markup for Adding User

It has five TextBox and one button for adding a new user as shown in the image below. ResultFor the source code of the image above. Kindly copy and paste to your BODY tag of your page.
  1. <!---Adding User-->
  2. <form method="post" name="form" action="">
  3. <table class="table_one" cellpadding="4" cellspacing="4">
  4. <tr>
  5. <td colspan="2" style="text-align:center; font-size:20px; color:blue; font-weight:bold;">Add User</td>
  6. </tr>
  7. <tr>
  8. <td>
  9. <span style="color:blue;">User ID</span>
  10. </td>
  11. <td>
  12. <input name="userid" style="font-size:15px;" placeholder="User ID..." type="text" id="userid" required />
  13. </td>
  14. </tr>
  15. <tr>
  16. <td>
  17. <span style="color:blue;">Name</span>
  18. </td>
  19. <td>
  20. <input name="name" style="font-size:15px;" placeholder="Name..." type="text" id="name" required />
  21. </td>
  22. </tr>
  23. <tr>
  24. <td>
  25. <span style="color:blue;">Username</span>
  26. </td>
  27. <td>
  28. <input name="username" type="text" style="font-size:15px;" placeholder="Username..." id="username" required />
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <span style="color:blue;">Email</span>
  34. </td>
  35. <td>
  36. <input name="email" type="email" style="font-size:15px;" placeholder="Email..." id="email" required />
  37. </td>
  38. </tr>
  39. <tr>
  40. <td>
  41. <span style="color:blue;">Address</span>
  42. </td>
  43. <td>
  44. <input name="address" style="font-size:15px;" placeholder="Address..." type="text" id="address" required />
  45. </td>
  46. </tr>
  47. <tr>
  48. <td align="center" colspan="2">
  49. <input type="submit" style="color:blue; background-color:azure; border:1px groove blue; border-radius:4px; font-size:18px; cursor:pointer;" name="submit" value="Add Employee" id="submit" />
  50. </td>
  51. </tr>
  52. </tbody>
  53. </form>
After adding the user, we are going to show or display the data in the table.

Creating Table for Displaying Data

We are going to show our user data in the table to execute the DELETE Statement using a checkbox as you can see in the image below. ResultThis is the source code of the image below.
  1. <!---Deleting Multiple Data and Viewing-->
  2. <form method="post">
  3. <table border="1" class="table_two" cellpadding="0" cellspacing="0" id="container">
  4. <tr align="center" style="color:blue; font-family:cursive;">
  5. <td>
  6. <input type="checkbox" onClick="return sel(this);"/>&nbsp;&nbsp;&nbsp;<span>Select All</span>
  7. </td>
  8. <td>
  9. <strong>User ID</strong>
  10. </td>
  11. <td>
  12. <strong>Name</strong>
  13. </td>
  14. <td>
  15. <strong>Username</strong>
  16. </td>
  17. <td>
  18. <strong>Email</strong>
  19. </td>
  20. <td>
  21. <strong>Address</strong>
  22. </td>
  23. </tr>
  24.  
  25. <?php
  26. $result=mysql_query("select * from user order by id ASC ") or die(mysql_error());
  27. while($user_info=mysql_fetch_array($result)){
  28. ?>
  29.  
  30. <tr align="center">
  31. <td>
  32. <input type="checkbox" name="check[]" value="<?php echo $user_info['id']; ?>" id="all" />
  33. </td>
  34. <td>
  35. <?php echo $user_info['userid']; ?>
  36. </td>
  37. <td>
  38. <?php echo $user_info['name']; ?>
  39. </td>
  40. <td>
  41. <?php echo $user_info['username']; ?>
  42. </td>
  43. <td>
  44. <?php echo $user_info['email']; ?>
  45. </td>
  46. <td>
  47. <?php echo $user_info['address']; ?>
  48. </td>
  49. </tr>
  50. <?php } ?>
  51. <tr>
  52. <td colspan="6">
  53. <p align="center">
  54. <input type="submit" style="color:blue; background-color:azure; border:1px groove blue; border-radius:4px; font-size:18px; cursor:pointer;" name="delete" value="Delete User Account" />
  55. </p>
  56. </td>
  57. </tr>
  58.  
  59. </table>
  60. </form>
After creating the Markup, we are going to create our PHP Query for INSERT and DELETE Statement. This is the PHP Query for inserting and deleting data in our database table.
  1. <?php
  2. $db = mysql_connect('localhost','root','') or die ("Unable to connect to Database Server.");
  3. mysql_select_db ('delete_multiple', $db) or die ("Could not select database.");
  4. if(isset($_POST['submit']))
  5. {
  6. $userid=$_POST['userid'];
  7. $name=$_POST['name'];
  8. $username=$_POST['username'];
  9. $email=$_POST['email'];
  10. $address=$_POST['address'];
  11. $insert=mysql_query("insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')");
  12. header ('location:index.php');
  13.  
  14. // if($insert)
  15. // {
  16. // echo "<script>alert('User has been added'); window.location='index.php'</script>";
  17. // }
  18.  
  19. }
  20.  
  21. if(isset($_POST['delete']))
  22. {
  23. $check=$_POST['check'];
  24. $count=count($check);
  25. for($i=0;$i<$count;$i++){
  26. $del_id = $check[$i];
  27. $delete=mysql_query("delete from user where id='$del_id'") or die(mysql_error());
  28. }
  29. if($delete)
  30. {
  31. echo "<script>alert('User/s has been deleted'); window.location='index.php'</script>";
  32. }
  33. }
  34. ?>
Hope that this tutorial will help you a lot. If you are interested in programming, we have an example of programs that may help you even just in small ways. If you want more tutorials, you can visit our website, click here Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment