欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用户信息添加、修改、删除

程序员文章站 2022-03-07 19:53:19
...

用户信息添加、修改、删除

添加用户信息 adduser.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>添加用户</title>
  8. <style>
  9. .form {
  10. width: 360px;
  11. margin: 0 auto;
  12. padding: 10px;
  13. }
  14. form {
  15. display: grid;
  16. gap: 0.5rem;
  17. }
  18. fieldset {
  19. display: grid;
  20. gap: 0.5rem;
  21. }
  22. h2 {
  23. text-align: center;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="form">
  29. <h2>添加用户</h2>
  30. <form action="createuser.php" method="post">
  31. <fieldset>
  32. <legend>必填项</legend>
  33. <div>
  34. <label for="account">用名:</label>
  35. <input id="account" type="text" name="account" required />
  36. </div>
  37. <div>
  38. <label for="password">密码:</label>
  39. <input type="password" id="password" name="password" required />
  40. </div>
  41. </fieldset>
  42. <fieldset>
  43. <legend>选填项</legend>
  44. <div>
  45. <label for="name">姓名:</label>
  46. <input type="text" id="name" name="name" />
  47. </div>
  48. <div>
  49. <label for="email">邮箱:</label>
  50. <input id="email" type="email" name="email" />
  51. </div>
  52. <div>
  53. <label for="phone">电话:</label>
  54. <input id="phone" type="number" name="phone" />
  55. </div>
  56. </fieldset>
  57. <div>
  58. <label for="phone">状态:</label>
  59. <select name="state" id="">
  60. <option value="1">开启</option>
  61. <option value="2">关闭</option>
  62. </select>
  63. </div>
  64. <button>添加</button>
  65. </form>
  66. </div>
  67. </body>
  68. </html>

用户信息添加、修改、删除

数据库链接,创建PDO对象文件 linkdb.php

  1. <?php
  2. try{
  3. $pdo = new PDO('mysql:host=127.0.0.1;dbname=prodb','root','123456');
  4. }catch(PDOException $e){
  5. // 抛出错误,错误是你可以定义的
  6. echo '数据库连接失败' . $e->getMessage();
  7. }

添加用户信息后台文件createuser.php

  1. <?php
  2. require_once "linkdb.php";
  3. if(!empty($_POST)){
  4. $account=trim($_POST['account']);
  5. $password=md5(trim($_POST['password']));
  6. $name=trim($_POST['name'])??"";
  7. $email=trim($_POST['email'])??"";
  8. $phone=$_POST['phone'];
  9. // var_dump(is_integer($phone),$phone);
  10. $createTime=time();
  11. $updateTime=time();
  12. $state=$_POST['state'];
  13. $pdo->query('SET NAMES utf8');
  14. $sql="INSERT INTO `php_user` SET `account`=? ,`password`=?,";
  15. $sql.="`name`=?,`email`=?,`phone`=?,`create_time`=?,`update_time`=?,`state`=?";
  16. $pre=$pdo->prepare($sql);
  17. $exec=$pre->execute(
  18. [
  19. $account,
  20. $password,
  21. $name,
  22. $email,
  23. $phone,
  24. $createTime,
  25. $updateTime,
  26. $state
  27. ]
  28. );
  29. if($exec){
  30. echo $pre -> rowCount();
  31. echo '<hr>';
  32. echo $pdo -> lastInsertId();
  33. echo '<script>alert("添加成功");location.href="showuser.php"</script>';
  34. }else{
  35. print_r($pre->errorInfo());
  36. }
  37. }

显示用户信息文件 showuser.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>显示用户信息</title>
  8. <style>
  9. /* 1. 添加表格线,添加给单元格 */
  10. tbody td,
  11. th {
  12. border: 1px solid;
  13. }
  14. /* 2. 折叠表格线 */
  15. table {
  16. border-collapse: collapse;
  17. width: 90%;
  18. /* 对齐 */
  19. text-align: center;
  20. margin: 2em auto;
  21. }
  22. /* 标题 */
  23. table caption {
  24. font-weight: bolder;
  25. margin-bottom: 1em;
  26. }
  27. table thead {
  28. background-color: lightgreen;
  29. }
  30. tbody tr:nth-of-type(2n){
  31. background-color: #ddd;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <?php
  37. require_once "linkdb.php";
  38. $sql='SELECT * FROM `php_user`';
  39. $pre=$pdo->prepare($sql);
  40. $pre -> bindColumn('id',$id);
  41. $pre -> bindColumn('account',$account);
  42. $pre -> bindColumn('name',$name);
  43. $pre -> bindColumn('phone',$phone);
  44. $pre -> bindColumn('email',$email);
  45. $pre -> bindColumn('create_time',$create_time);
  46. $pre -> bindColumn('update_time',$update_time);
  47. $pre -> bindColumn('state',$state);
  48. $exec=$pre->execute();
  49. ?>
  50. <div class="show">
  51. <table>
  52. <caption>用户信息</caption>
  53. <thead>
  54. <tr>
  55. <th><input type="checkbox" id="check_all"></th>
  56. <th>ID</th>
  57. <th>用户名</th>
  58. <th>姓名</th>
  59. <th>电话</th>
  60. <th>邮箱</th>
  61. <th>创建时间</th>
  62. <th>最后登录时间</th>
  63. <th>状态</th>
  64. </tr>
  65. </thead>
  66. <tbody>
  67. <?php while ($pre->fetch(PDO::FETCH_ASSOC)):?>
  68. <tr>
  69. <td><input type="checkbox" class="chk_item"></td>
  70. <td class="id"><?=$id?></td>
  71. <td><a href="edituser.html?id=<?=$id?>" title="点击修改用户信息" ><?=$account?></a></td>
  72. <td><?=$name?></td>
  73. <td><?=$phone?></td>
  74. <td><?=$email?></td>
  75. <td><?=date('Y-m-d',$create_time)?></td>
  76. <td><?=date('Y-m-d',$update_time)?></td>
  77. <td><?=$state==1?'开启':'关闭'?></td>
  78. </tr>
  79. <?php endwhile ?>
  80. </tbody>
  81. <tfoot>
  82. <tr><td colspan="3"><button onclick="del()">删除</button></td>
  83. <td><a href="adduser.html">添加用户</a></td>
  84. </tr>
  85. </tfoot>
  86. </table>
  87. </div>
  88. <script>
  89. const chkAll=document.querySelector("#check_all");
  90. const chkItems=document.querySelectorAll(".chk_item");
  91. chkAll.onchange=ev=>chkItems.forEach(item=>item.checked=ev.target.checked);
  92. chkItems.forEach(item=>item.onchange=()=>chkAll.checked=[...chkItems].every(item=>item.checked));
  93. function del(){
  94. if(confirm('您确定要删除这些用户吗?')){
  95. const chkItems=document.querySelectorAll(".chk_item");
  96. chkItems.forEach(item=>{
  97. if(item.checked){
  98. const id=item.parentElement.nextElementSibling.textContent;
  99. let isDel=delOpt(id);
  100. if(isDel){
  101. // console.log( item.parentElement.parentElement);
  102. item.parentElement.parentElement.remove();
  103. }
  104. }
  105. })
  106. }else{
  107. alert("NO");
  108. }
  109. }
  110. async function delOpt(id){
  111. const response = await fetch("del_user.php?id=" + id);
  112. const comments = await response.json();
  113. return comments;
  114. }
  115. </script>
  116. </body>
  117. </html>

用户信息添加、修改、删除

  • 选中,点击删除
    用户信息添加、修改、删除

删除用户文件 del_user.php

  1. <?php
  2. require_once "linkdb.php";
  3. $id=$_GET['id'];
  4. $sql="DELETE FROM `php_user` WHERE id=$id";
  5. $pre=$pdo->prepare($sql);
  6. $exec=$pre->execute();
  7. if($exec){
  8. echo 1;
  9. }else{echo 0;}

修改用户信息 页面文件edituser.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>更新用户</title>
  8. <style>
  9. .form {
  10. width: 360px;
  11. margin: 0 auto;
  12. padding: 10px;
  13. }
  14. form {
  15. display: grid;
  16. gap: 0.5rem;
  17. }
  18. fieldset {
  19. display: grid;
  20. gap: 0.5rem;
  21. }
  22. h2 {
  23. text-align: center;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="form">
  29. <h2>更新用户信息</h2>
  30. <form action="updateuser.php" method="post">
  31. <fieldset>
  32. <legend>用户信息</legend>
  33. <div>
  34. <input type="hidden" name="id" />
  35. <label for="account">用名:</label>
  36. <input id="account" type="text" name="account" required readonly />
  37. </div>
  38. <div>
  39. <label for="name">姓名:</label>
  40. <input type="text" id="name" name="name" />
  41. </div>
  42. <div>
  43. <label for="email">邮箱:</label>
  44. <input id="email" type="email" name="email" />
  45. </div>
  46. <div>
  47. <label for="phone">电话:</label>
  48. <input id="phone" type="number" name="phone" />
  49. </div>
  50. </fieldset>
  51. <div>
  52. <label for="phone">状态:</label>
  53. <select name="state" id="">
  54. <option value="1">开启</option>
  55. <option value="2">关闭</option>
  56. </select>
  57. </div>
  58. <button>修改</button>
  59. </form>
  60. </div>
  61. <script>
  62. let url = location.search;
  63. let id = url.substr(4);
  64. if (id > 0) {
  65. fetch("searchbyid.php?id=" + id)
  66. .then((response) => response.json())
  67. .then((json) => {
  68. const form = document.forms[0];
  69. form.id.value = json.id;
  70. form.account.value = json.account;
  71. form.account.readonly = true;
  72. form.phone.value = json.phone;
  73. form.email.value = json.email;
  74. form.name.value = json.name;
  75. form.state.value = json.state;
  76. });
  77. } else {
  78. }
  79. </script>
  80. </body>
  81. </html>

用户信息添加、修改、删除

修改用户信息 后台文件updateuser.php

  1. <?php
  2. require_once "linkDB.php";
  3. if(!empty($_POST)){
  4. $id=$_POST['id'];
  5. $account=trim($_POST['account']);
  6. $name=trim($_POST['name'])??"";
  7. $email=trim($_POST['email'])??"";
  8. $phone=$_POST['phone'];
  9. // var_dump(is_integer($phone),$phone);
  10. $updateTime=time();
  11. $state=$_POST['state'];
  12. $pdo->query('SET NAMES utf8');
  13. $sql="UPDATE `php_user` SET `name`=?,`email`=?,`phone`=?,`update_time`=?,`state`=? WHERE id=?";
  14. $pre=$pdo->prepare($sql);
  15. $exec=$pre->execute(
  16. [
  17. $name,
  18. $email,
  19. $phone,
  20. $updateTime,
  21. $state,
  22. $id
  23. ]
  24. );
  25. if($exec){
  26. echo $pre -> rowCount();
  27. echo '<hr>';
  28. echo $pdo -> lastInsertId();
  29. echo '<script>alert("修改成功");location.href="showuser.php"</script>';
  30. }else{
  31. print_r($pre->errorInfo());
  32. }
  33. }