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

使用面向对象方法实现用户信息增删改查

程序员文章站 2022-03-24 09:44:58
...

auto.php

  1. <?php
  2. spl_autoload_register(
  3. function($className){
  4. // $className 是new的Son这个名字,这个名字是类名,不是文件名
  5. require $className . '.php';
  6. }
  7. );

创建PDO文件 CreatePDO.php

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

模型文件 User.php

  1. <?php
  2. class User{
  3. public $id;
  4. public $account;
  5. public $password;
  6. public $name;
  7. public $phone;
  8. public $email;
  9. public $createtime;
  10. public $updatetime;
  11. public $state;
  12. }

操作文件 UserContr.php

  1. <?php
  2. require_once "auto.php";
  3. class UserContr{
  4. /**
  5. *添加用户
  6. */
  7. public static function createUser($user){
  8. $sql="INSERT INTO `php_user` SET `account`=? ,`password`=?,";
  9. $sql.="`name`=?,`email`=?,`phone`=?,`createtime`=?,`updatetime`=?,`state`=?";
  10. $pdo =CreatePDO::Create();
  11. $pre=$pdo->prepare($sql);
  12. $exec=$pre->execute(
  13. [
  14. $user->account,
  15. $user->password,
  16. $user->name,
  17. $user->email,
  18. $user->phone,
  19. $user->createTime,
  20. $user->updateTime,
  21. $user->state
  22. ]
  23. );
  24. if($exec){
  25. return 1;
  26. }else{
  27. return 0;
  28. }
  29. }
  30. // 根据id查询用户信息
  31. public static function GetUserById($id){
  32. $sql="SELECT * FROM `php_user` WHERE `id`=$id";
  33. $pdo =CreatePDO::Create();
  34. $pre=$pdo->prepare($sql);
  35. $exec=$pre->execute();
  36. $data = $pre -> fetch();
  37. return $data;
  38. }
  39. // 所有用户信息
  40. public static function GetAllUser(){
  41. $sql="SELECT * FROM `php_user`";
  42. $pdo =CreatePDO::Create();
  43. $pre=$pdo->prepare($sql);
  44. $exec=$pre->execute();
  45. $data = $pre -> fetchAll(PDO::FETCH_OBJ);
  46. return $data;
  47. }
  48. // 根据id删除用户信息
  49. public static function DeleteById($id){
  50. $pdo =CreatePDO::Create();
  51. $sql="DELETE FROM `php_user` WHERE id=$id";
  52. $pre=$pdo->prepare($sql);
  53. $exec=$pre->execute();
  54. if($exec){
  55. $res=1;
  56. }else{$res=0;}
  57. return $res;
  58. }
  59. // 修改用户信息
  60. public static function UpdateUser($user){
  61. $pdo =CreatePDO::Create();
  62. $sql="UPDATE `php_user` SET `name`=?,`email`=?,`phone`=?,`updatetime`=?,`state`=? WHERE id=?";
  63. $pre=$pdo->prepare($sql);
  64. $exec=$pre->execute(
  65. [
  66. $user->name,
  67. $user->email,
  68. $user->phone,
  69. $user->updateTime,
  70. $user->state,
  71. $user->id
  72. ]
  73. );
  74. if($exec){
  75. return 1;
  76. }else{
  77. return 0;
  78. }
  79. }
  80. }

添加用户信息文件 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. label {
  26. margin-left: 40px;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="form">
  32. <h2>添加用户</h2>
  33. <form action="createuser.php" method="post">
  34. <fieldset>
  35. <legend>必填项</legend>
  36. <div>
  37. <label for="account">用名:</label>
  38. <input id="account" type="text" name="account" required />
  39. </div>
  40. <div>
  41. <label for="password">密码:</label>
  42. <input type="password" id="password" name="password" required />
  43. </div>
  44. </fieldset>
  45. <fieldset>
  46. <legend>选填项</legend>
  47. <div>
  48. <label for="name">姓名:</label>
  49. <input type="text" id="name" name="name" />
  50. </div>
  51. <div>
  52. <label for="email">邮箱:</label>
  53. <input id="email" type="email" name="email" />
  54. </div>
  55. <div>
  56. <label for="phone">电话:</label>
  57. <input id="phone" type="number" name="phone" />
  58. </div>
  59. </fieldset>
  60. <div>
  61. <label for="phone">状态:</label>
  62. <select name="state" id="">
  63. <option value="1">开启</option>
  64. <option value="2">关闭</option>
  65. </select>
  66. </div>
  67. <button>添加</button>
  68. </form>
  69. </div>
  70. </body>
  71. </html>

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

  1. <?php
  2. require_once "auto.php";
  3. if(!empty($_POST)){
  4. $user=new User();
  5. $user->account=trim($_POST['account']);
  6. $user->password=md5(trim($_POST['password']));
  7. $user->name=trim($_POST['name'])??"";
  8. $user->email=trim($_POST['email'])??"";
  9. $user->phone=$_POST['phone'];
  10. $user->createTime=time();
  11. $user->updateTime=time();
  12. $user->state=$_POST['state'];
  13. if(UserContr::createUser($user)){
  14. echo '<script>alert("添加成功");location.href="showuser.php"</script>';
  15. }else{
  16. print_r($pre->errorInfo());
  17. }
  18. }

显示用户信息文件 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 "auto.php";
  38. $data=UserContr::GetAllUser();
  39. ?>
  40. <div class="show">
  41. <table>
  42. <caption>用户信息</caption>
  43. <thead>
  44. <tr>
  45. <th><input type="checkbox" id="check_all"></th>
  46. <th>ID</th>
  47. <th>用户名</th>
  48. <th>姓名</th>
  49. <th>电话</th>
  50. <th>邮箱</th>
  51. <th>创建时间</th>
  52. <th>最后登录时间</th>
  53. <th>状态</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <?php foreach($data as $user): ?>
  58. <tr>
  59. <td><input type="checkbox" class="chk_item"></td>
  60. <td class="id"><?=$user->id?></td>
  61. <td><a href="edituser.html?id=<?=$user->id?>" title="点击修改用户信息" ><?=$user->account?></a></td>
  62. <td><?=$user->name?></td>
  63. <td><?=$user->phone?></td>
  64. <td><?=$user->email?></td>
  65. <td><?=date('Y-m-d',$user->createtime)?></td>
  66. <td><?=date('Y-m-d',$user->updatetime)?></td>
  67. <td><?=$user->state==1?'开启':'关闭'?></td>
  68. </tr>
  69. <?php endforeach ?>
  70. </tbody>
  71. <tfoot>
  72. <tr><td colspan="3"><button onclick="del()">删除</button></td>
  73. <td><a href="adduser.html">添加用户</a></td>
  74. </tr>
  75. </tfoot>
  76. </table>
  77. </div>
  78. <script>
  79. const chkAll=document.querySelector("#check_all");
  80. const chkItems=document.querySelectorAll(".chk_item");
  81. chkAll.onchange=ev=>chkItems.forEach(item=>item.checked=ev.target.checked);
  82. chkItems.forEach(item=>item.onchange=()=>chkAll.checked=[...chkItems].every(item=>item.checked));
  83. function del(){
  84. if(confirm('您确定要删除这些用户吗?')){
  85. const chkItems=document.querySelectorAll(".chk_item");
  86. chkItems.forEach(item=>{
  87. if(item.checked){
  88. const id=item.parentElement.nextElementSibling.textContent;
  89. let isDel=delOpt(id);
  90. if(isDel){
  91. // console.log( item.parentElement.parentElement);
  92. item.parentElement.parentElement.remove();
  93. }
  94. }
  95. })
  96. }else{
  97. alert("NO");
  98. }
  99. }
  100. async function delOpt(id){
  101. const response = await fetch("del_user.php?id=" + id);
  102. const comments = await response.json();
  103. return comments;
  104. }
  105. </script>
  106. </body>
  107. </html>

删除用户文件 del_user.php

  1. <?php
  2. require_once "auto.php";
  3. $id=$_GET['id'];
  4. $res=UserContr::DeleteById($id);
  5. echo $res;

修改用户信息 页面文件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 "auto.php";
  3. if(!empty($_POST)){
  4. $user=new User();
  5. $user->id=$_POST['id'];
  6. $user->account=trim($_POST['account']);
  7. $user->name=trim($_POST['name'])??"";
  8. $user->email=trim($_POST['email'])??"";
  9. $user->phone=$_POST['phone'];
  10. $user->updateTime=time();
  11. $user->state=$_POST['state'];
  12. $res=UserContr::UpdateUser($user);
  13. if($res){
  14. echo '<script>alert("修改成功");location.href="showuser.php"</script>';
  15. }else{
  16. }