Update and Delete Multiple User Account In PHP

In this tutorial we will teach you how to create and implement a Update and Delete Multiple User Account from the database using PHP. To select or identify multiple user accounts, we have to used check boxes and passed the ID of each user which were brought from the database to the check box assigned to each user in a html form. To submit the selected check boxes which holds the ID of each user detail for an update or deletion purpose, javascript is used for redirection to the pages with the scripts that performs the actions needed for each operation. See the sample code below.

Sample Code

Index.php - This script of file is compose of PHP and Html Form, this file is for displaying the users accounts from the database through the html form so the user will not go directly in the mysql database and they can easily viewed through the html interface.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Update & Delete Multiple User Account In PHP</title>
  5. <link rel="stylesheet" type="text/css" href="css/styles.css" />
  6. <script language="javascript" type="text/javascript">
  7. function update_data()
  8. {
  9. document.form_name.action = "users-update.php";
  10. document.form_name.submit();
  11. }
  12. function delete_data()
  13. {
  14. if(confirm("Do you really mean to delete the details?"))
  15. {
  16. document.form_name.action = "users-deletion.php";
  17. document.form_name.submit();
  18. }
  19. }
  20. </script>
  21. </head>
  22. <?php
  23. include "config.php";
  24. $users_details = mysql_query("select * from `user_update` order by `id` asc");
  25. ?>
  26. <body>
  27. <div style="width:500px; margin:0 auto; margin-top: 100px;">
  28. <form name="form_name" method="post" action="">
  29. <div class="header_title" align="center"><h1>Update & Delete Multiple User Account In PHP</h1></div>
  30. <table border="0" cellpadding="10" cellspacing="1" width="500" class="table_wrapper">
  31. <tr class="table_header">
  32. <td>First Name</td>
  33. <td>Last Name</td>
  34. <td>Username</td>
  35. <td>Password</td>
  36. <td style="text-align:center;">Action</td>
  37. </tr>
  38. <?php
  39. $tclass = "dark";
  40. while($get_data = mysql_fetch_array($users_details))
  41. {
  42. ?>
  43. <tr class="<?php echo isset($tclass) ? $tclass : ''; ?>">
  44. <td><?php echo trim(strip_tags($get_data["firstname"])); ?></td>
  45. <td><?php echo trim(strip_tags($get_data["lastname"])); ?></td>
  46. <td><?php echo trim(strip_tags($get_data["username"])); ?></td>
  47. <td><?php echo trim(strip_tags($get_data["password"])); ?></td>
  48. <td style="text-align:center;"><input type="checkbox" name="users[]" class="users" id="users" value="<?php echo trim(strip_tags($get_data["id"])); ?>" ></td>
  49. </tr>
  50. <?php
  51. }
  52. ?>
  53. <tr class="table_header">
  54. <td colspan="5" align="center">
  55. <input class="button" type="button" name="update" value="Update" onClick="update_data();" />
  56. <input class="button" type="button" name="delete" value="Delete" onClick="delete_data();" />
  57. </td>
  58. </tr>
  59. </table>
  60. </form>
  61. </div>
  62. </body>
  63. </html>
User_Update.php - This script is for the updating the users acount or information.
  1. <?php
  2. include "config.php";
  3. if(isset($_POST["submit"]) && trim($_POST["submit"]) != "")
  4. {
  5. $counted_users = count($_POST["userId"]);
  6. echo $counted_users;
  7. for($i=0; $i<$counted_users; $i++)
  8. {
  9. mysql_query("update `user_update` set `firstname` = '".mysql_real_escape_string(trim(strip_tags($_POST["firstname"][$i])))."', `lastname` = '".mysql_real_escape_string(trim(strip_tags($_POST["lastname"][$i])))."', `username` = '".mysql_real_escape_string(trim(strip_tags($_POST["username"][$i])))."', `password` = '".mysql_real_escape_string(trim(strip_tags($_POST["password"][$i])))."' where `id` = '".mysql_real_escape_string(trim(strip_tags($_POST["userId"][$i])))."'");
  10. }
  11. header("location:index.php");
  12. }
  13. ?>
  14. <!DOCTYPE html>
  15. <html>
  16. <head>
  17. <title>Update & Delete Multiple User Account In PHP</title>
  18. <link rel="stylesheet" type="text/css" href="css/styles.css" />
  19. </head>
  20. <body>
  21. <div style="width:523px; margin: 0 auto; margin-top: 100px;" align="center">
  22. <div class="header_title"><h1>Update & Delete Multiple User Account In PHP</h1></div>
  23. <?php
  24. $users_id = count($_POST["users"]);
  25. if($users_id == 0)
  26. {
  27. ?>
  28. <div style="margin:0 auto; padding:10px; background:#FFFFEA; line-height:20px; border:1px solid #F1F1F1;">It seems you did not check any item to update.<br>Please click on the button below to go back and then check on an item to update.</div><br><br>
  29. <div align="center" class="button" onClick="window.location.replace('index.php');">Back</div>
  30. <?php
  31. }
  32. else
  33. {
  34. ?>
  35. <form name="frmUser" method="post" action="<?php echo isset($_SERVER['PHP_SELF']) ? trim($_SERVER['PHP_SELF']) : ""; ?>">
  36. <table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="table_wrapper">
  37. <tr class="table_header">
  38. <td>Update User Information</td>
  39. </tr>
  40. <?php
  41. for($i=0; $i<$users_id; $i++)
  42. {
  43. $users_details = mysql_query("select * from `user_update` where `id` = '".mysql_real_escape_string(trim(strip_tags($_POST["users"][$i])))."'");
  44. $getdetail[$i] = mysql_fetch_array($users_details);
  45. ?>
  46. <tr>
  47. <td>
  48. <table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="inner_table">
  49. <tr>
  50. <td><label>First Name</label></td>
  51. <td><input type="text" name="firstname[]" class="field" value="<?php echo $getdetail[$i]['firstname']; ?>"></td>
  52. </tr>
  53. <tr>
  54. <td><label>Last Name</label></td>
  55. <td><input type="text" name="lastname[]" class="field" value="<?php echo $getdetail[$i]['lastname']; ?>"></td>
  56. </tr>
  57. <tr>
  58. <td><label>Username</label></td>
  59. <td>
  60. <input type="hidden" name="userId[]" class="field" value="<?php echo $getdetail[$i]['id']; ?>">
  61. <input type="text" name="username[]" class="field" value="<?php echo $getdetail[$i]['username']; ?>"></td>
  62. </tr>
  63. <tr>
  64. <td><label>Password</label></td>
  65. <td><input type="password" name="password[]" class="field" value="<?php echo $getdetail[$i]['password']; ?>"></td>
  66. </tr>
  67. </table>
  68. </td>
  69. </tr>
  70. <?php
  71. }
  72. ?>
  73. <tr>
  74. <td colspan="2" align="center"><input type="submit" name="submit" value="Save Changes" class="button"></td>
  75. </tr>
  76. </table>
  77. </form>
  78. <?php
  79. }
  80. ?>
  81. </div>
  82. </body>
  83. </html>
resultUser_Deletion.php - This short script of code is for deleting a one or group of users account in the database.
  1. <?php
  2. include "config.php";
  3. $users_id = count($_POST["users"]);
  4. for($i=0; $i<$users_id; $i++)
  5. {
  6. mysql_query("delete from `user_update` where `id` = '".mysql_real_escape_string(trim(strip_tags($_POST["users"][$i])))."'");
  7. }
  8. header("location: index.php");
  9. ?>
Hope that you learn in this project and enjoy coding. Don't forget to LIKE & SHARE this website.

Add new comment