How To Make Delete Multiple Rows Using PHP

Language

Good Day!!!

In this tutorial, we are going to learn on How To Make Delete Multiple Rows Using PHP. The features of this project is to add a data then, delete multiple data using check box as our selector. Hope you find this useful.

Directions:

For Javascript - Select All Rows
  1. <script language="JavaScript">
  2. function sel(source)
  3. {
  4. checkboxes = document.getElementsByName('check[]');
  5. for(var i in checkboxes)
  6. checkboxes[i].checked = source.checked;
  7. }
  8. </script>
For PHP - Add Data
  1. if(isset($_POST['submit']))
  2. {
  3.  $userid=$_POST['userid'];
  4.  $name=$_POST['name'];
  5.  $username=$_POST['username'];
  6.  $email=$_POST['email'];
  7.  $address=$_POST['address'];
  8.  $id1 = $_SERVER['REMOTE_ADDR'];
  9.  $insert=mysql_query("insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')");
  10.  if($insert)
  11.  {
  12.         echo "<script>alert('User has been added'); window.location='index.php'</script>";
  13.  }
  14.  
  15. }
For PHP - Delete Multiple Rows of Data
  1. if(isset($_POST['delete']))
  2. {
  3. $check=$_POST['check'];
  4. $count=count($check);
  5. for($i=0;$i<$count;$i++){
  6. $del_id = $check[$i];
  7. $delete=mysql_query("delete from user where id='$del_id'") or die(mysql_error());
  8.  }
  9. if($delete)
  10. {
  11.         echo "<script>alert('User has been deleted'); window.location='index.php'</script>";
  12. }
  13.        
  14. }
Full Source Code
  1. <!DOCTYPE html>
  2. <title>Delete Multiple Rows</title>
  3. <script language="JavaScript">
  4. function sel(source)
  5. {
  6. checkboxes = document.getElementsByName('check[]');
  7. for(var i in checkboxes)
  8. checkboxes[i].checked = source.checked;
  9. }
  10. <style type="text/css">
  11. body {
  12.         width:100%;
  13.         font-family:"Trebuchet MS";
  14.         margin:0;
  15.         padding:0;
  16. }
  17.  
  18. h2 a {
  19.         text-decoration:none;
  20.         color:#06F;
  21. }
  22.  
  23. #container {
  24.         width:800px;
  25.         margin:0 auto;
  26.         margin-top:30px;
  27. }
  28. </head>
  29.  
  30. <?php
  31. $db = mysql_connect('localhost','root','') or die ("Unable to connect to Database Server.");
  32. mysql_select_db ('demo', $db) or die ("Could not select database.");
  33. if(isset($_POST['submit']))
  34. {
  35. $userid=$_POST['userid'];
  36. $name=$_POST['name'];
  37. $username=$_POST['username'];
  38. $email=$_POST['email'];
  39. $address=$_POST['address'];
  40. $id1 = $_SERVER['REMOTE_ADDR'];
  41. $insert=mysql_query("insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')");
  42. if($insert)
  43. {
  44.         echo "<script>alert('User has been added'); window.location='index.php'</script>";
  45.  }
  46.  
  47. }
  48. if(isset($_POST['delete']))
  49. {
  50. $check=$_POST['check'];
  51. $count=count($check);
  52. for($i=0;$i<$count;$i++){
  53. $del_id = $check[$i];
  54. $delete=mysql_query("delete from user where id='$del_id'") or die(mysql_error());
  55. }
  56. if($delete)
  57. {
  58.         echo "<script>alert('User has been deleted'); window.location='index.php'</script>";
  59. }
  60.        
  61. }
  62. ?>
  63.  
  64.  
  65. <form method="post" name="form" action="">
  66. <table border="1" style="border:4px groove #CCC; float:left; margin-left:80px;" cellpadding="4" cellspacing="4">
  67.         <tbody>
  68.                 <tr>
  69.                         <td colspan="2" style="text-align:center; font-weight:bold;">Add User</td>
  70.                 </tr>
  71.                 <tr>
  72.                         <td>
  73.                                 <span style="color:blue;">User ID</span>
  74.                         </td>
  75.                         <td>
  76.                                 <input name="userid" style="font-size:15px;" placeholder="User ID..." type="text" id="userid" required />
  77.                         </td>
  78.                 </tr>
  79.                 <tr>
  80.                         <td>
  81.                                 <span style="color:blue;">Name</span>
  82.                         </td>
  83.                         <td>
  84.                                 <input name="name" style="font-size:15px;" placeholder="Name..." type="text" id="name" required />
  85.                         </td>
  86.                 </tr>
  87.                 <tr>
  88.                         <td>
  89.                                 <span style="color:blue;">Username</span>
  90.                         </td>
  91.                         <td>
  92.                                 <input name="username" type="text" style="font-size:15px;" placeholder="Username..." id="username" required />
  93.                         </td>
  94.                 </tr>
  95.                 <tr>
  96.                         <td>
  97.                                 <span style="color:blue;">Email</span>
  98.                         </td>
  99.                         <td>
  100.                                 <input name="email" type="email" style="font-size:15px;" placeholder="Email..." id="email" required />
  101.                         </td>
  102.                 </tr>
  103.                 <tr>
  104.                         <td>
  105.                                 <span style="color:blue;">Address</span>
  106.                         </td>
  107.                         <td>
  108.                                 <input name="address" style="font-size:15px;" placeholder="Address..." type="text" id="address" required />
  109.                         </td>
  110.                 </tr>
  111.                 <tr>
  112.                         <td align="center" colspan="2">
  113.                                 <input type="submit" style="color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;" name="submit" value="Add Employee" id="submit" />
  114.                         </td>
  115.                 </tr>
  116.         </tbody>
  117. </form>
  118.  
  119. <!---Viewing-->
  120. <form method="post">
  121.         <table border="1" style="border:4px groove #CCC; float:right; margin-right:80px;" cellpadding="0" cellspacing="0"  id="container">
  122.                 <tr align="center">
  123.                         <td>
  124.                                 <input type="checkbox"  onClick="return sel(this);"/>&nbsp;&nbsp;&nbsp;<span>Select All</span>
  125.                         </td>
  126.                         <td>
  127.                                 <strong>User ID</strong>
  128.                         </td>
  129.                         <td>
  130.                                 <strong>Name</strong>
  131.                         </td>
  132.                         <td>
  133.                                 <strong>Username</strong>
  134.                         </td>
  135.                         <td>
  136.                                 <strong>Email</strong>
  137.                         </td>
  138.                         <td>
  139.                                 <strong>Address</strong>
  140.                         </td>
  141.                 </tr>
  142.        
  143. <?php
  144. $id1 = $_SERVER['REMOTE_ADDR'];
  145. $result=mysql_query("select * from user order by id ASC ") or die(mysql_error());
  146. while($user_info=mysql_fetch_array($result)){
  147. ?>
  148.        
  149.                 <tr align="center">
  150.                         <td>
  151.                                 <input type="checkbox" name="check[]"  value="<?php echo $user_info['id']; ?>" id="all" />
  152.                         </td>
  153.                         <td>
  154.                                 <?php echo $user_info['userid']; ?>
  155.                         </td>
  156.                         <td>
  157.                                 <?php echo $user_info['name']; ?>
  158.                         </td>
  159.                         <td>
  160.                                 <?php echo $user_info['username']; ?>
  161.                         </td>
  162.                         <td>
  163.                                 <?php echo $user_info['email']; ?>
  164.                         </td>
  165.                         <td>
  166.                                 <?php echo $user_info['address']; ?>
  167.                         </td>
  168.                 </tr>
  169. <?php } ?>
  170.                 <tr>
  171.                         <td colspan="6">
  172.                                 <p align="center">
  173.                                         <input type="submit" style="color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;" name="delete" value="Delete" />
  174.                                 </p>
  175.                         </td>
  176.                 </tr>
  177.        
  178.         </table>
  179.        
  180.        
  181. </form>
  182.  
  183.  
  184. </body>
  185. </html>
So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you.

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.

Comments

how to do to be able to add a rar file that you can download in the table and how to do to be able to add one picture któro would wyświetlało also in the table.

I mean something like this photo Please help in obtaining effect Regards

Add new comment