Blogger Information
Blog 20
fans 0
comment 1
visits 13140
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
结合课程知识完成用户列表的修改和删除功能
zg的php学习
Original
447 people have browsed it

结合课程知识完成用户列表的修改和删除功能

工具类: DB.php

  1. <?php
  2. class DB{
  3. public static $config = array(); //数据库连接的配置信息
  4. public static $pdo = null; //PDO连接标识符
  5. public static $stmt = null; //PDOStatement类。预处理sql语句及存放结果集
  6. public static $sql = null; //最后执行的sql语句
  7. public static $lastInsertId = null;//最后一次插入操作的自增值(AUTO_INCREMANT)
  8. public static $error=null; //错误信息
  9. /**
  10. * 构造函数, 实例化时用来连接数据库
  11. * @param array 数据库配置项
  12. */
  13. public function __construct($dbConfig=''){
  14. //实例化时,如果未传参数或参数非数组格式则连接默认数据库
  15. if(!is_array($dbConfig)){
  16. $dbConfig = [
  17. 'dbms'=>'mysql',
  18. 'host'=>'10.10.1.242',
  19. 'dbname'=>'php',
  20. 'user'=>'root',
  21. 'password'=>'130599',
  22. 'charset'=>'utf8'
  23. ];
  24. }
  25. //判断是否配置了host项(数据库)
  26. if(empty($dbConfig['host'])){
  27. self::throw_exception("没有配置数据库,请先配置!");
  28. }
  29. self::$config = $dbConfig;
  30. if(!isset(self::$pdo)){
  31. try{
  32. if(self::$config['dbms']=='mysql'){
  33. $dsn = 'mysql:host=' . self::$config['host'] . ';dbname=' . self::$config['dbname'];
  34. }else if(self::$config['dbms']=='sqlsrv'){
  35. $dsn = 'sqlsrv:server=' . self::$config['host'] . ';database=' . self::$config['dbname'];
  36. }
  37. self::$pdo = new PDO($dsn,self::$config['user'],self::$config['password']);
  38. }catch(PDOException $e){
  39. self::throw_exception($e->getMessage());
  40. }
  41. }
  42. if(!self::$pdo){
  43. self::throw_exception("PDO连接错误!");
  44. return false;
  45. }
  46. }
  47. /**
  48. *获取单条记录
  49. *@param string 要执行的sql语句
  50. *@return array 结果集(一维数组)
  51. */
  52. public static function fetch($sql=null){
  53. if($sql!=null)self::query($sql);
  54. $res = self::$stmt->fetch(constant("PDO::FETCH_ASSOC"));
  55. return $res;
  56. }
  57. /**
  58. * 获取所有记录
  59. * @param string 要执行的sql语句
  60. * @return array 结果集(二维数组)
  61. */
  62. public static function fetchAll($sql=null){
  63. if($sql!=null)self::query($sql);
  64. $res = self::$stmt->fetchAll(constant("PDO::FETCH_ASSOC"));
  65. return $res;
  66. }
  67. /**
  68. * 执行查询
  69. * @param 要查询的sql语句
  70. * @return boolean
  71. */
  72. public static function query($sql=''){
  73. if(!self::$pdo)return false; //如果PDO未连接则返回
  74. if(!empty(self::$stmt))self::free(); //如果$stmt之前有结果集则先释放
  75. self::$sql = $sql;
  76. self::$stmt = self::$pdo->prepare(self::$sql);
  77. $res = self::$stmt->execute();
  78. self::haveErrorThrowException();
  79. return $res;
  80. }
  81. /**
  82. * 执行增删改操作,返回受影响的行数
  83. * @param string 要执行的sql语句
  84. * @return number 受影响的行数 *
  85. */
  86. public static function execute($sql=null){
  87. if(!self::$pdo)return false;//如果PDO未连接则返回
  88. if(!empty(self::$stmt))self::free(); //如果$stmt之前有结果集则先释放
  89. if($sql!=null)self::$sql = $sql;
  90. $res = self::$pdo->exec($sql);
  91. self::haveErrorThrowException();
  92. if($res){
  93. self::$lastInsertId = self::$pdo->lastInsertId();
  94. return $res;
  95. }else{
  96. return false;
  97. }
  98. }
  99. /**
  100. * 释放结果集
  101. */
  102. public static function free(){
  103. self::$stmt = null;
  104. }
  105. /**
  106. * 自定义错误消息
  107. * @param string 错误消息
  108. */
  109. public static function throw_exception($msg){
  110. echo $msg;
  111. }
  112. /**
  113. * 抛出错误信息
  114. *
  115. * @return boolean ( description_of_the_return_value )
  116. */
  117. public static function haveErrorThrowException(){
  118. $obj = empty(self::$stmt) ? self::$pdo : self::$stmt;
  119. $arrError = $obj->errorInfo();
  120. if($arrError[0]!='00000'){
  121. self::$error = 'SQLSTATE=>'.$arrError[0].'<br/>SQL Error=>'.$arrError[2].'<br/>Error SQL=>'.self::$sql;
  122. self::throw_exception(self::$error);
  123. return false;
  124. }
  125. if(self::$sql==''){
  126. self::throw_exception('没有可执行的SQL语句');
  127. return false;
  128. }
  129. }
  130. }

列表页: user_list.php

  1. <?php
  2. require_once 'auto.php';
  3. $db = new DB();
  4. $data = DB::fetchAll('select * from user');
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8" />
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  12. <title>用户列表</title>
  13. <style>
  14. .list {
  15. margin:1em auto;
  16. border-collapse: collapse;
  17. }
  18. .title{
  19. text-align:center;
  20. margin-top:2em;
  21. }
  22. .list th,
  23. .list td {
  24. border: 1px solid black;
  25. font-size:14px;
  26. padding: 2px 5px;
  27. }
  28. .list a{
  29. background-color: #f3f3f3;
  30. border-radius:10px;
  31. padding:0 3px;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <h2 class="title">用户列表</h2>
  37. <table class="list">
  38. <thead>
  39. <tr>
  40. <th>id</th>
  41. <th>帐号</th>
  42. <th>姓名</th>
  43. <th>年龄</th>
  44. <th>手机号码</th>
  45. <th>注册时间</th>
  46. <th>登录时间</th>
  47. <th>状态</th>
  48. <th>操作</th>
  49. </tr>
  50. </thead>
  51. <tbody>
  52. <?php
  53. foreach($data as $row){
  54. ?>
  55. <tr>
  56. <td><?= $row['id'] ?></td>
  57. <td><?= $row['account'] ?></td>
  58. <td><?= $row['name'] ?></td>
  59. <td><?= $row['age'] ?></td>
  60. <td><?= $row['phone'] ?></td>
  61. <td><?= $row['add_time'] ?></td>
  62. <td><?= $row['last_time'] ?></td>
  63. <td><?= $row['status'] ?></td>
  64. <td><a href="user_curd.php?type=m&id=<?= $row['id'] ?>">修改</a> <a href="user_curd.php?type=d&id=<?= $row['id'] ?>">删除</a></td>
  65. </tr>
  66. <?php
  67. }
  68. ?>
  69. </tbody>
  70. </table>
  71. </body>
  72. </html>

user_list


修改删除操作页: user_curd.php

  1. <?php
  2. require_once 'auto.php';
  3. $type = $_REQUEST['type'];
  4. $id = $_REQUEST['id'];
  5. if(empty($_POST)){ //GET
  6. if($type == 'm'){ //修改
  7. $db = new DB();
  8. $data = DB::fetch("select * from user where id = " . $id);
  9. }else if($type == 'd'){ //删除
  10. $db = new DB();
  11. $res = DB::execute("delete from user where id = " . $id);
  12. if($res){
  13. echo "<script>alert('删除成功!');window.location='user_list.php';</script>";
  14. }else{
  15. echo "<script>alert('没有任何数据被修改!');window.location='user_list.php';</script>";
  16. }
  17. return;
  18. }else{
  19. echo '无效的请求!';
  20. return false;
  21. }
  22. }else{
  23. //POST
  24. $data['account'] = trim($_POST['account']);
  25. $data['password'] = trim($_POST['pwd']);
  26. $data['name'] = trim($_POST['username']);
  27. $data['age'] = trim($_POST['age']);
  28. $data['phone'] = trim($_POST['phone']);
  29. $data['add_time'] = strtotime($_POST['atime']);
  30. $data['last_time'] = strtotime($_POST['ltime']);
  31. $data['status'] = $_POST['status'];
  32. if(empty($data['account']) or empty($data['password'])){
  33. echo '帐号或密码不能为空!';
  34. }else{
  35. //拼接sql语句
  36. $sql = "update user set ";
  37. foreach($data as $key=>$value){
  38. $sql .= $key . " = '" . $value . "',";
  39. }
  40. $sql = rtrim($sql,',');
  41. $sql .= " where id = " . $id;
  42. //执行修改操作
  43. $db = new DB();
  44. $res = DB::execute($sql);
  45. if($res){
  46. // header("Refresh:2;url=user_list.php");
  47. echo "<script>alert('修改成功!即将返回用户列表页..');window.location='user_list.php';</script>";
  48. }else{
  49. echo "<script>alert('修改失败!');</script>";
  50. }
  51. }
  52. }
  53. ?>
  54. <!DOCTYPE html>
  55. <html lang="en">
  56. <head>
  57. <meta charset="UTF-8" />
  58. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  59. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  60. <title>用户列表</title>
  61. <style>
  62. * {
  63. box-sizing: border-box;
  64. }
  65. .box {
  66. width: 400px;
  67. background-color: #f9f9f9;
  68. margin: 0 auto;
  69. border-radius: 10px;
  70. }
  71. .form {
  72. width: 400px;
  73. padding: 1em 1.5em 0.5em 1em;
  74. display: grid;
  75. grid-template-columns: 25% 75%;
  76. gap: 0.5em;
  77. }
  78. .title {
  79. text-align: center;
  80. margin-top: 1em;
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <h2 class="title">用户信息修改</h2>
  86. <div class="box">
  87. <form action="" method="post" class="form">
  88. <label>id</label>
  89. <input type="text" value="<?= $data['id'] ?>" name="id" readonly disabled />
  90. <label for="account">帐号</label>
  91. <input
  92. type="text"
  93. name="account"
  94. id="account"
  95. required
  96. placeholder="帐号不为空..."
  97. value="<?= $data['account'] ?>"
  98. />
  99. <label for="pwd">密码</label>
  100. <input
  101. type="password"
  102. name="pwd"
  103. id="pwd"
  104. required
  105. placeholder="密码不能为空..."
  106. value="<?= $data['password'] ?>"
  107. />
  108. <label for="username">姓名</label>
  109. <input type="text" name="username" id="username" value="<?= $data['name'] ?>" />
  110. <label for="age">年龄</label>
  111. <input type="text" name="age" id="age" value="<?= $data['age'] ?>" />
  112. <label for="phone">手机号码</label>
  113. <input type="text" name="phone" id="phone" value="<?= $data['phone'] ?>" />
  114. <label for="atime">注册时间</label>
  115. <input type="text" name="atime" id="atime" value="<?= date('Y-m-d',$data['add_time']) ?>"/>
  116. <label for="ltime">登录时间</label>
  117. <input type="text" name="ltime" id="ltime" value="<?= date('Y-m-d H:i:s',$data['last_time']) ?>"/>
  118. <label for="status">状态</label>
  119. <select name="status" id="status">
  120. <option value="1" <?= $data['status'] == '1' ? 'selected' : ''; ?> >开启</option>
  121. <option value="0" <?= $data['status'] == '0' ? 'selected' : ''; ?> >关闭</option>
  122. </select>
  123. <label for=""></label>
  124. <input type="submit" value="提交" />
  125. </form>
  126. </div>
  127. </body>
  128. </html>

user_curd


修改操作截图:

3

4

5

删除操作截图:

6

7

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post