Make Delete Multiple Rows v2 PHP Script


Make Delete Multiple Rows v2 PHP Script

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.

 

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. }

 

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. }

 

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

 

 

 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"