Blogger Information
Blog 19
fans 0
comment 0
visits 13336
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP用数据库类实现数据库增删改查
王浩
Original
1151 people have browsed it
  1. 作业内容:
  2. 1、练习课上代码
  3. 2、看看函数库
  4. 3、预习类
  5. 4、修改用户功能(根据添加来改)
  6. 5、删除用户功能

先上图



FileName : index.php(列表)

  1. <?php
  2. require_once 'Mypdo.class.php';
  3. $pdo = new Mypdo("mysql", "localhost", "study", "root", "root");
  4. $pdo->connect();
  5. $res = $pdo->select("user","*","","id","0,10");
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>用户列表</title>
  14. <style>
  15. table{
  16. width: 100%;
  17. border-collapse:collapse;
  18. }
  19. th, td{
  20. padding: 5px;
  21. border: 1px solid #336699;
  22. }
  23. th{
  24. font-size: 20px;
  25. font-weight: bolder;
  26. background: #ccc;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <h2>用户列表</h2><hr/>
  32. <button style="margin-bottom:8px;" onclick="add();">添加用户</button>
  33. <table>
  34. <tr>
  35. <th>ID</th>
  36. <th>帐号</th>
  37. <th>姓名</th>
  38. <th>年龄</th>
  39. <th>电话</th>
  40. <th>创建时间</th>
  41. <th>最后登陆时间</th>
  42. <th>状态</th>
  43. <th>操作</th>
  44. </tr>
  45. <?php foreach($res as $k=>$v){ ?>
  46. <tr>
  47. <td><?=$v['id'] ?></td>
  48. <td><?=$v['account'] ?></td>
  49. <td><?=$v['name'] ?></td>
  50. <td><?=$v['age'] ?></td>
  51. <td><?=$v['phone'] ?></td>
  52. <td><?=date('Y-m-d', $v['add_time']) ?></td>
  53. <td><?=date('Y-m-d', $v['last_time']) ?></td>
  54. <td><?=$v['status']?"正常":"禁用" ?></td>
  55. <td><a href="javascript:mod(<?=$v['id'] ?>);">修改</a> | <a href="javascript:del(<?=$v['id'] ?>);">删除</a></td>
  56. </tr>
  57. <?php } ?>
  58. </table>
  59. <script>
  60. //添加用户
  61. function add(){
  62. this.location = "add.php";
  63. }
  64. //修改用户
  65. function mod(id){
  66. this.location = "mod.php?id=" + id;
  67. }
  68. //删除用户
  69. function del(id){
  70. if(confirm("确定要删除该用户吗?")){
  71. this.location = "del.php?id=" + id;
  72. }else{
  73. return false;
  74. }
  75. }
  76. </script>
  77. </body>
  78. </html>

FileName: add.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. body{
  10. background: #eee;
  11. }
  12. div{
  13. padding: 2px;
  14. background: #ccc;
  15. width: 400px;
  16. height: 260px;
  17. position: absolute;
  18. left:50%;
  19. top:50%;
  20. margin-left:-200px;
  21. margin-top:-130px;
  22. border-radius:4px;
  23. box-shadow: 4px 4px 4px 4px #666;
  24. }
  25. div *{
  26. color:#336699;
  27. text-align: center;
  28. }
  29. input[type="submit"]{
  30. margin-top:8px;
  31. width: 220px;
  32. height: 30px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div>
  38. <h1>添加用户</h1>
  39. <form method="post">
  40. 账户:<input name="account" type="text" /><br/>
  41. 密码:<input name="password" type="password" /><br/>
  42. 姓名:<input name="name" type="text" /><br/>
  43. 年龄:<input name="age" type="number" /><br/>
  44. 手机:<input name="phone" type="text" /><br/>
  45. <input type="submit" value="添加" />
  46. </form>
  47. </div>
  48. </body>
  49. </html>
  50. <?php
  51. // 1、判断是否是post提交
  52. if(!empty($_POST)){
  53. // 2、账户必须填写,如果不填写,就会报错
  54. if(empty($_POST['account'])){
  55. echo '<div style="color:red;margin-top:20px;">请输入账户</div>';
  56. exit;
  57. }
  58. // 3、密码必须添加
  59. if(empty($_POST['password'])){
  60. echo '<div style="color:red;margin-top:20px;">请输入密码</div>';
  61. exit;
  62. }
  63. $_POST['password'] = md5($_POST['password']);
  64. $data = [
  65. "add_time" => time(),
  66. "last_time" => time(),
  67. "status" => 1
  68. ];
  69. require_once 'Mypdo.class.php';
  70. $pdo = new Mypdo("mysql", "localhost", "study", "root", "root");
  71. $pdo->connect();
  72. if($pdo->insert("user", array_merge($data, $_POST))){
  73. echo '<script>alert("添加成功"); this.location="index.php"; </script>';
  74. }else{
  75. echo '<script>alert("添加失败");</script>';
  76. }
  77. }
  78. ?>

FileName : mod.php(修改)

  1. <?php
  2. if(!isset($_GET['id'])){
  3. die("参数有误");
  4. }
  5. require_once 'Mypdo.class.php';
  6. $pdo = new Mypdo("mysql", "localhost", "study", "root", "root");
  7. $pdo->connect();
  8. $res = $pdo->find("user","*","id={$_GET['id']}");
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <title>修改用户</title>
  17. <style>
  18. body{
  19. background: #eee;
  20. }
  21. div{
  22. padding: 2px;
  23. background: #ccc;
  24. width: 400px;
  25. height: 260px;
  26. position: absolute;
  27. left:50%;
  28. top:50%;
  29. margin-left:-200px;
  30. margin-top:-130px;
  31. border-radius:4px;
  32. box-shadow: 4px 4px 4px 4px #666;
  33. }
  34. div *{
  35. color:#336699;
  36. text-align: center;
  37. }
  38. input[type="submit"]{
  39. margin-top:8px;
  40. width: 220px;
  41. height: 30px;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div>
  47. <h1>修改用户</h1>
  48. <form method="post">
  49. 账户:<input name="account" type="text" value="<?=$res['account'] ?>" /><br/>
  50. 姓名:<input name="name" type="text" value="<?=$res['name'] ?>" /><br/>
  51. 年龄:<input name="age" type="number" value="<?=$res['age'] ?>" /><br/>
  52. 手机:<input name="phone" type="text" value="<?=$res['phone'] ?>" /><br/>
  53. <select name="status" id="status">
  54. <?php
  55. for($i=0;$i<2;$i++){
  56. echo '<option value="'.$i.'" '.($i==$res['status']?" selected":"") .'>'.($i?"正常":"禁用").'</option>';
  57. }
  58. ?>
  59. </select><br>
  60. <input type="submit" value="修改" />
  61. </form>
  62. </div>
  63. </body>
  64. </html>
  65. <?php
  66. // 1、判断是否是post提交
  67. if(!empty($_POST)){
  68. // 2、账户必须填写,如果不填写,就会报错
  69. if(empty($_POST['account'])){
  70. echo '<div style="color:red;margin-top:20px;">请输入账户</div>';
  71. exit;
  72. }
  73. require_once 'Mypdo.class.php';
  74. $pdo = new Mypdo("mysql", "localhost", "study", "root", "root");
  75. $pdo->connect();
  76. if($pdo->update("user", $_POST, "id=".$_GET['id'])){
  77. echo '<script>alert("修改成功"); this.location="index.php"; </script>';
  78. }else{
  79. echo '<script>alert("修改失败");</script>';
  80. }
  81. }
  82. ?>

FileName:del.php(删除)

  1. <?php
  2. require_once 'Mypdo.class.php';
  3. $pdo = new Mypdo("mysql", "localhost", "study", "root", "root");
  4. $pdo->connect();
  5. if($pdo->delete("user","id=".$_GET['id'])){
  6. echo '<script>alert("删除成功"); this.location="index.php"; </script>';
  7. }else{
  8. echo '<script>alert("删除失败");</script>';
  9. }
  10. ?>

Mypdo.class.php(引用类库)

  1. <?php
  2. /**
  3. * Pdo操作数据库类
  4. * 2021年10月19日
  5. */
  6. class Mypdo{
  7. private $db_type; //数据库类型
  8. private $db_host; //数据库主机
  9. private $db_name; //数据库名
  10. private $db_user; //连接数据库帐号
  11. private $db_password; //连接数据库密码
  12. private $pdo; //连接句柄
  13. public function __construct($type, $host, $name, $user, $password, $charset = 'utf-8')
  14. {
  15. // 存储相关参数
  16. $this->db_type = $type;
  17. $this->db_host = $host;
  18. $this->db_name = $name;
  19. $this->db_user = $user;
  20. $this->db_password = $password;
  21. }
  22. public function connect(){
  23. $dsn = "{$this->db_type}:host={$this->db_host}; dbname={$this->db_name}";//数据源
  24. // echo $dsn;
  25. // exit;
  26. try {
  27. //实例化PDO类,创建PDO对象
  28. $this->pdo = new PDO($dsn,$this->db_user, $this->db_password);
  29. } catch (PDOException $e) {
  30. die('数据库错误:'.$e->getMessage());
  31. }
  32. return $this->pdo;
  33. }
  34. // 按指定条件,查询多条记录
  35. public function select($table, $fields='*', $where='', $order='', $limit=''){
  36. //创建SQL语句
  37. $sql = 'SELECT ';
  38. if (is_array($fields)) { // 支持数组
  39. foreach ($fields as $field) {
  40. $sql .= $field.', ';
  41. }
  42. } else { // 也支持字符串
  43. $sql .= $fields;
  44. }
  45. //去掉最后的豆号和空格
  46. $sql = rtrim(trim($sql),',');
  47. $sql .= ' FROM '.$table;
  48. //查询条件:暂只支持字符串
  49. if(!empty($where)){
  50. $sql .= ' WHERE '.$where;
  51. }
  52. //排序条件,暂未支持顺序和倒序,默认用倒序吧,新添加的放最前面
  53. if(!empty($order)) {
  54. $sql .= ' order by '.$order.' DESC';
  55. }
  56. //分页条件
  57. if(!empty($limit)) {
  58. $sql .= ' limit '.$limit;
  59. }
  60. $sql .= ';';
  61. //创建PDO预处理对象
  62. $stmt = $this->pdo->prepare($sql);
  63. //执行查询操作
  64. if($stmt->execute()){
  65. if($stmt->rowCount()>0){
  66. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  67. //返回一个二维数组
  68. return $stmt->fetchAll();
  69. }
  70. } else {
  71. return false;
  72. }
  73. }
  74. // 查询单条记录
  75. public function find($table,$fields,$where=''){
  76. //创建SQL语句
  77. $sql = 'SELECT ';
  78. if (is_array($fields)) {
  79. foreach ($fields as $field) {
  80. $sql .= $field.', ';
  81. }
  82. } else {
  83. $sql .= $fields;
  84. }
  85. $sql = rtrim(trim($sql),',');
  86. $sql .= ' FROM '.$table;
  87. //查询条件
  88. if(!empty($where)){
  89. $sql .= ' WHERE '.$where;
  90. }
  91. $sql .= ' LIMIT 1;';
  92. //创建PDO预处理对象
  93. $stmt = $this->pdo->prepare($sql);
  94. //执行查询操作
  95. if($stmt->execute()){
  96. if($stmt->rowCount()>0){
  97. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  98. return $stmt->fetch();
  99. }
  100. } else {
  101. return false;
  102. }
  103. }
  104. //插入数据
  105. public function insert($table,$data=[]){
  106. //创建SQL语句
  107. $sql = "INSERT INTO {$table} SET ";
  108. //组装插入语句
  109. if(is_array($data)){
  110. foreach ($data as $k=>$v) {
  111. $sql .= $k.'="'.$v.'", ';
  112. }
  113. }else{
  114. return false;
  115. }
  116. //去掉尾部逗号,并添加分号结束
  117. $sql = rtrim(trim($sql),',').';';
  118. //创建PDO预处理对象
  119. $stmt = $this->pdo->prepare($sql);
  120. //执行新增操作
  121. if($stmt->execute()){
  122. if($stmt->rowCount()>0){
  123. return true;
  124. }
  125. } else {
  126. return false;
  127. }
  128. }
  129. // 更新数据
  130. public function update($table,$data=[], $where='') {
  131. //创建SQL语句
  132. $sql = "UPDATE {$table} SET ";
  133. //组装修改语句
  134. if(is_array($data)){
  135. foreach ($data as $k=>$v) {
  136. $sql .= $k.'="'.$v.'", ';
  137. }
  138. }
  139. //去掉尾部逗号,并添加分号结束
  140. $sql = rtrim(trim($sql),',');
  141. //查询条件
  142. if(!empty($where)){
  143. $sql .= ' WHERE '.$where;
  144. }
  145. //创建PDO预处理对象
  146. $stmt = $this->pdo->prepare($sql);
  147. //执行新增操作
  148. if($stmt->execute()){
  149. if($stmt->rowCount()>0){
  150. return true;
  151. }
  152. } else {
  153. return false;
  154. }
  155. }
  156. // 删除数据
  157. public function delete($table,$where=''){
  158. //创建SQL语句
  159. $sql = "DELETE FROM {$table} ";
  160. //查询条件
  161. if(!empty($where)){
  162. $sql .= ' WHERE '.$where;
  163. }else{ //加上这一句。以防没有条件,删除了所有数据
  164. return false;
  165. }
  166. //创建PDO预处理对象
  167. $stmt = $this->pdo->prepare($sql);
  168. //执行删除操作
  169. if($stmt->execute()){
  170. if($stmt->rowCount()>0){
  171. return true;
  172. }
  173. } else {
  174. return false;
  175. }
  176. }
  177. }
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