博客列表 >用户添加,修改,删除

用户添加,修改,删除

可乐
可乐原创
2021年10月27日 01:05:47538浏览

连接数据库

  1. <?php
  2. try{
  3. $pdo = new PDO('mysql:locahost=127.0.0.1;dbname=php','root','123456');
  4. }catch(PDOException $e){
  5. echo '没有连接' . $e->getMessage();
  6. }
  7. $pre = $pdo -> prepare('SELECT * FROM `user`');
  8. $exe = $pre -> execute();
  9. $data = $pre -> fetchAll();
  10. ?>

用户信息

  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. </head>
  9. <body>
  10. <?php
  11. require_once "db.php";
  12. $sql='SELECT * FROM `user`';
  13. $pre=$pdo->prepare($sql);
  14. $pre -> bindColumn('id',$id);
  15. $pre -> bindColumn('account',$account);
  16. $pre -> bindColumn('email',$email);
  17. $pre -> bindColumn('add_time',$add_time);
  18. $pre -> bindColumn('last_time',$last_time);
  19. $pre -> bindColumn('status',$status);
  20. $exec=$pre->execute();
  21. ?>
  22. <div class="user">
  23. <table>
  24. <caption>用户信息</caption>
  25. <thead>
  26. <tr>
  27. <th><input type="checkbox" id="check_all"></th>
  28. <th>ID</th>
  29. <th>账号</th>
  30. <th>邮箱</th>
  31. <th>注册时间</th>
  32. <th>登录时间</th>
  33. <th>状态</th>
  34. </tr>
  35. </thead>
  36. <tbody>
  37. <?php while ($pre->fetch(PDO::FETCH_ASSOC)):?>
  38. <tr>
  39. <td><input type="checkbox" class="chk_item"></td>
  40. <td class="id"><?=$id?></td>
  41. <td><a href="edit.php ?id=<?=$id?>" title="点击修改"><?=$account?></a></td>
  42. <td><?=$email?></td>
  43. <td><?=date('Y-m-d',$add_time)?></td>
  44. <td><?=date('Y-m-d',$last_time)?></td>
  45. <td><?=$status==1?'开启':'关闭'?></td>
  46. </tr>
  47. <?php endwhile ?>
  48. </tbody>
  49. <tfoot>
  50. <tr>
  51. <td colspan="3"><button onclick="del()">删除</button></td>
  52. <td><a href="add.php">注册用户</a></td>
  53. </tr>
  54. </tfoot>
  55. </table>
  56. </div>
  57. <script>
  58. const chkAll = document.querySelector("#check_all");
  59. const chkItems = document.querySelectorAll(".chk_item");
  60. chkAll.onchange = ev => chkItems.forEach(item => item.checked = ev.target.checked);
  61. chkItems.forEach(item => item.onchange = () => chkAll.checked = [...chkItems].every(item => item.checked));
  62. function del() {
  63. if (confirm('确定要删除吗?')) {
  64. const chkItems = document.querySelectorAll(".chk_item");
  65. chkItems.forEach(item => {
  66. if (item.checked) {
  67. const id = item.parentElement.nextElementSibling.textContent;
  68. let isDel = delOpt(id);
  69. if (isDel) {
  70. item.parentElement.parentElement.remove();
  71. }
  72. }
  73. })
  74. } else {
  75. alert("NO");
  76. }
  77. }
  78. async function delOpt(id) {
  79. const response = await fetch("del.php?id=" + id);
  80. const comments = await response.json();
  81. return comments;
  82. }
  83. </script>
  84. </body>
  85. </html>

用户添加

  1. <?php
  2. require_once "db.php";
  3. $sql='SELECT * FROM `user`';
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <title>添加用户</title>
  12. </head>
  13. <body>
  14. <form method="post">
  15. 账户:<input name="account" type="text" /><br />
  16. 密码:<input name="password" type="password" /><br />
  17. 邮箱:<input name="email" type="text" /><br />
  18. <input type="submit" value="注册" />
  19. </form>
  20. </body>
  21. </html>
  22. <?php
  23. if(!empty($_POST)){
  24. if(empty($_POST['account'])){
  25. echo '<div style="color:blue;margin-top:20px;">请输入账户</div>';
  26. exit;
  27. }
  28. if(empty($_POST['password'])){
  29. echo '<div style="color:red;margin-top:20px;">请输入密码</div>';
  30. exit;
  31. }
  32. $pdo = new PDO('mysql:localhost=127.0.0.1;dbname=php','root','123456');
  33. $sql = 'INSERT INTO `user` SET `account` = "' . $_POST['account'] . '", `password` = "' . md5($_POST['password']) .'"';
  34. if(!empty($_POST['email'])){
  35. $sql .= ', `email` = "' . $_POST['email'] . '"';
  36. }
  37. $sql .= ', `last_time` = ' . time();
  38. $sql .= ', `status` = 1';
  39. $pre = $pdo->prepare($sql);
  40. $pre -> execute();
  41. if($pre->rowCount() > 0){
  42. echo '<script>alert("注册成功");window.location.href="list.php"</script>';
  43. exit;
  44. }else{
  45. echo '<div style="color:blue;margin-top:20px;">注册失败,请重试</div>';
  46. exit;
  47. }
  48. }
  49. ?>

用户修改

  1. <?php
  2. if(!empty($_POST)){
  3. if(empty($_POST['account'])){
  4. echo '<div style="color:blue;margin-top:20px;">请输入账户</div>';
  5. exit;
  6. }
  7. if(empty($_POST['password'])){
  8. echo '<div style="color:red;margin-top:20px;">请输入密码</div>';
  9. exit;
  10. }
  11. $pdo = new PDO('mysql:localhost=127.0.0.1;dbname=php','root','123456');
  12. $sql = 'UPDATE `user` SET `account` = "' . $_POST['account'] . '", `password` = "' . md5($_POST['password']) .'"';
  13. if(!empty($_POST['email'])){
  14. $sql .= ', `email` = "' . $_POST['email'] . '"';
  15. }
  16. $sql .= ', `last_time` = ' . time();
  17. $sql .= ', `status` = 1';
  18. $pre = $pdo->prepare($sql);
  19. $pre -> execute();
  20. if($pre->rowCount() > 0){
  21. echo '<script>alert("修改成功");window.location.href="list.php"</script>';
  22. exit;
  23. }else{
  24. echo '<div style="color:blue;margin-top:20px;">修改失败,请重试</div>';
  25. exit;
  26. }
  27. }
  28. ?>

用户删除

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

运行效果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议