• We just launched and are currently in beta. Join us as we build and grow the community.

Update and Delete Multiple User Account In PHP

Phoenixz241

Deployment Pipeline Designer
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
162
Likes
49
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
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. session_start

    (

    )

    ;
  3. include

    "config.php"

    ;
  4. if

    (

    isset

    (

    $_POST

    [

    "submit"

    ]

    )

    &&

    trim

    (

    $_POST

    [

    "submit"

    ]

    )

    !=

    ""

    )
  5. {
  6. $counted_users

    =

    count

    (

    $_POST

    [

    "userId"

    ]

    )

    ;
  7. echo

    $counted_users

    ;
  8. for

    (

    $i

    =

    0

    ;

    $i

    <

    $counted_users

    ;

    $i

    ++

    )
  9. {
  10. 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

    ]

    )

    )

    )

    .

    "'"

    )

    ;
  11. }
  12. header

    (

    "location:index.php"

    )

    ;
  13. }
  14. ?>
  15. <!DOCTYPE html>
  16. <html>
  17. <head>
  18. <title>Update & Delete Multiple User Account In PHP</title>
  19. <link rel="stylesheet" type="text/css" href="css/styles.css" />
  20. </head>
  21. <body>
  22. <div style="width:523px; margin: 0 auto; margin-top: 100px;" align="center">
  23. <div class="header_title"><h1>Update & Delete Multiple User Account In PHP</h1></div>
  24. <?php
  25. $users_id

    =

    count

    (

    $_POST

    [

    "users"

    ]

    )

    ;
  26. if

    (

    $users_id

    ==

    0

    )
  27. {
  28. ?>
  29. <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>
  30. <div align="center" class="button" onClick="window.location.replace('index.php');">Back</div>
  31. <?php
  32. }
  33. else
  34. {
  35. ?>
  36. <form name="frmUser" method="post" action="<?php

    echo

    isset

    (

    $_SERVER

    [

    'PHP_SELF'

    ]

    )

    ? trim

    (

    $_SERVER

    [

    'PHP_SELF'

    ]

    )

    :

    ""

    ;

    ?>

    ">
  37. <table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="table_wrapper">
  38. <tr class="table_header">
  39. <td>Update User Information</td>
  40. </tr>
  41. <?php
  42. for

    (

    $i

    =

    0

    ;

    $i

    <

    $users_id

    ;

    $i

    ++

    )
  43. {
  44. $users_details

    =

    mysql_query

    (

    "select * from `user_update` where `id` = '"

    .

    mysql_real_escape_string

    (

    trim

    (

    strip_tags

    (

    $_POST

    [

    "users"

    ]

    [

    $i

    ]

    )

    )

    )

    .

    "'"

    )

    ;
  45. $getdetail

    [

    $i

    ]

    =

    mysql_fetch_array

    (

    $users_details

    )

    ;
  46. ?>
  47. <tr>
  48. <td>
  49. <table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="inner_table">
  50. <tr>
  51. <td><label>First Name</label></td>
  52. <td><input type="text" name="firstname[]" class="field" value="<?php

    echo

    $getdetail

    [

    $i

    ]

    [

    'firstname'

    ]

    ;

    ?>

    "></td>
  53. </tr>
  54. <tr>
  55. <td><label>Last Name</label></td>
  56. <td><input type="text" name="lastname[]" class="field" value="<?php

    echo

    $getdetail

    [

    $i

    ]

    [

    'lastname'

    ]

    ;

    ?>

    "></td>
  57. </tr>
  58. <tr>
  59. <td><label>Username</label></td>
  60. <td>
  61. <input type="hidden" name="userId[]" class="field" value="<?php

    echo

    $getdetail

    [

    $i

    ]

    [

    'id'

    ]

    ;

    ?>

    ">
  62. <input type="text" name="username[]" class="field" value="<?php

    echo

    $getdetail

    [

    $i

    ]

    [

    'username'

    ]

    ;

    ?>

    "></td>
  63. </tr>
  64. <tr>
  65. <td><label>Password</label></td>
  66. <td><input type="password" name="password[]" class="field" value="<?php

    echo

    $getdetail

    [

    $i

    ]

    [

    'password'

    ]

    ;

    ?>

    "></td>
  67. </tr>
  68. </table>
  69. </td>
  70. </tr>
  71. <?php
  72. }
  73. ?>
  74. <tr>
  75. <td colspan="2" align="center"><input type="submit" name="submit" value="Save Changes" class="button"></td>
  76. </tr>
  77. </table>
  78. </form>
  79. <?php
  80. }
  81. ?>
  82. </div>
  83. </body>
  84. </html>

up2_0.png

User_Deletion.php - This short script of code is for deleting a one or group of users account in the database.

  1. <?php
  2. session_start

    (

    )

    ;
  3. include

    "config.php"

    ;
  4. $users_id

    =

    count

    (

    $_POST

    [

    "users"

    ]

    )

    ;
  5. for

    (

    $i

    =

    0

    ;

    $i

    <

    $users_id

    ;

    $i

    ++

    )
  6. {
  7. mysql_query

    (

    "delete from `user_update` where `id` = '"

    .

    mysql_real_escape_string

    (

    trim

    (

    strip_tags

    (

    $_POST

    [

    "users"

    ]

    [

    $i

    ]

    )

    )

    )

    .

    "'"

    )

    ;
  8. }
  9. header

    (

    "location: index.php"

    )

    ;
  10. ?>

Hope that you learn in this project and enjoy coding. Don't forget to LIKE & SHARE this website.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,891

323,899

Top