博客列表 >仿php.cn分页,并实现数据编辑及删除

仿php.cn分页,并实现数据编辑及删除

简行
简行原创
2020年08月05日 11:16:55698浏览

仿php.cn分页,并实现数据编辑及删除

1.代码文档:

ID 文件名 注释
1 connect.php 连接数据库
2 control.php 跳转编辑页面,保存数据编辑及删除数据操作
3 edit.php 编辑页面
4 list.php 查询数据操作
5 user_list.php 用户列表页面

2.文件代码部分:

  • connect.php
  1. <?php
  2. //数据库参数
  3. $param =[
  4. 'DB_HOST'=>'localhost',
  5. 'DB_TYPE'=>'mysql',
  6. 'DB_NAME'=>'my_user',
  7. 'DB_PASSWORD'=>'root123',
  8. 'DB_USER'=>'root',
  9. 'DB_CHARSET'=>'utf8',
  10. 'DB_PORT'=>'3306'
  11. ];
  12. $dsn = "mysql:host=".$param['DB_HOST'].";dbname=".$param['DB_NAME'].";charset=".$param['DB_CHARSET'];
  13. //连接数据
  14. try{
  15. //连接数据款
  16. $pdo = new PDO($dsn,$param['DB_USER'],$param['DB_PASSWORD']);
  17. } catch(PDOException $e){
  18. //捕捉特定于数据库信息的PDOEXCEPTION 异常
  19. echo $e->getMessage();
  20. } catch(Throwable $e){
  21. //捕捉拥有Throwable接口的错误或者其他异常
  22. echo $e->getMessage();
  23. }
  • control.php
  1. <?php
  2. require "connect.php";
  3. $act = $_GET['act'];
  4. $id = $_GET['id'];
  5. switch($act){
  6. case 'edit':
  7. include "edit.php";
  8. break;
  9. case 'saveedit':
  10. $username =$_POST['username'];
  11. $phone =$_POST['phone'];
  12. if(!empty($username) && !empty($phone)){
  13. $sql = " UPDATE `mu_user` SET `username`=?,`phone`=? WHERE `id`=?";
  14. $stm = $pdo->prepare($sql);
  15. // $sql = 'UPDATE `mu_user` SET `username`=?, `phone`=? WHERE `id`=?';
  16. // $stm = $pdo->prepare($sql);
  17. $stm->bindParam(1,$username);
  18. $stm->bindParam(2,$phone);
  19. $stm->bindParam(3,$id);
  20. $stm->execute();
  21. // var_dump($stm->rowCount());
  22. if($stm->rowCount()==1){
  23. exit("
  24. <script>
  25. alert('保存成功');
  26. location.href='./user_list.php';
  27. </script>
  28. ");
  29. }else{
  30. exit("
  31. <script>
  32. alert('保存失败');
  33. location.href='control.php?act=edit&id=".$id."';
  34. </script>
  35. ");
  36. }
  37. }else{
  38. exit( "<script>alert('参数不能为空');location.href='control.php?act=edit&id=".$id."';</script>");
  39. }
  40. break;
  41. case 'del':
  42. $sql = "DELETE FROM `mu_user` WHERE `id`=?";
  43. $stm = $pdo->prepare($sql);
  44. $stm->bindParam(1,$id);
  45. $stm->execute();
  46. if($stm->rowCount()==1){
  47. exit("
  48. <script>
  49. alert('删除成功');
  50. location.href='./user_list.php';
  51. </script>
  52. ");
  53. }
  54. break;
  55. }
  • edit.php
  1. <?php
  2. require "connect.php";
  3. $id = $_GET['id'];
  4. $sql = "SELECT * FROM `mu_user` WHERE id=?";
  5. $stm = $pdo->prepare($sql);
  6. $stm->bindParam(1,$id);
  7. $stm->execute();
  8. $user = $stm->fetch(PDO::FETCH_ASSOC);
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>编辑页面</title>
  16. </head>
  17. <style>
  18. h1{
  19. text-align: center;
  20. }
  21. form{
  22. border: 1px solid rosybrown;
  23. background-color: sandybrown;
  24. width: 300px;
  25. margin: 0 auto;
  26. }
  27. form div{
  28. margin-top: 15px;
  29. margin-left: 10%;
  30. margin-bottom: 10px;
  31. }
  32. /* form>input{
  33. margin-top: 15px;
  34. margin-left: 20%;
  35. } */
  36. </style>
  37. <body>
  38. <h1>用户编辑</h1>
  39. <form action="./control.php?act=saveedit&id=<?=$id?>" method="POST">
  40. <div>
  41. <label for="username">姓名</label>
  42. <input type="text" name="username" id="username" value="<?php echo $user['username']?>">
  43. </div>
  44. <div>
  45. <label for="phone">电话</label>
  46. <input type="text" name="phone" id="phone" value="<?php echo $user['phone']?>">
  47. </div>
  48. <div>
  49. <div><input type="submit" value="保存"></div>
  50. </form>
  51. </body>
  52. </html>
  • list.php
  1. <?php
  2. require "connect.php";
  3. //每页几条
  4. $limit =5;
  5. //当前也是\
  6. $page = $_GET['p'];
  7. if(empty($page))$page = 1;
  8. //每页起始位置
  9. $start = ($page-1)*$limit;
  10. $sql = "SELECT * FROM `mu_user` LIMIT ?,?";
  11. $stm = $pdo->prepare($sql);
  12. //绑定LIMIT占位符必须传入第三个参数PDO::PARAM_INT
  13. $stm->bindParam(1,$start,PDO::PARAM_INT);
  14. $stm->bindParam(2,$limit,PDO::PARAM_INT);
  15. // $stm->bindParam(1,$limit);
  16. $stm->execute();
  17. // fetchAll — 返回一个包含结果集中所有行的数组
  18. $result = $stm->fetchAll(PDO::FETCH_ASSOC);
  19. $sql2 = "SELECT COUNT('id') as num FROM `mu_user` ";
  20. //总条数
  21. $num = $pdo->query($sql2)->fetch()['num'];
  22. //总页数
  23. $pages = ceil($num/$limit);
  • user_list.php
  1. <?php
  2. require "list.php";
  3. //当前获取网页地址
  4. $url = $_SERVER['PHP_SELF'];
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <title>用户列表</title>
  12. </head>
  13. <style>
  14. table{
  15. text-align: center;
  16. border:2px solid #222;
  17. border-collapse: collapse;
  18. margin: 0 auto;
  19. }
  20. tr:first-of-type{
  21. background-color:thistle;
  22. }
  23. td{
  24. padding: 10px;
  25. border:1px solid #000;
  26. }
  27. p{
  28. text-align: center;
  29. }
  30. a{
  31. text-align: center;
  32. border:1px solid #000;
  33. text-decoration: none;
  34. padding: 5px 10px;
  35. margin: 10px 2px;
  36. border-radius: 10%;
  37. }
  38. p>span{
  39. font-size: 1.5rem;
  40. margin: auto;
  41. padding: 10px;
  42. }
  43. .cla{
  44. background-color: lightskyblue;
  45. color: lightgoldenrodyellow;
  46. border: 1px solid lightskyblue;
  47. }
  48. </style>
  49. <body>
  50. <table>
  51. <caption>用户列表</caption>
  52. <tr>
  53. <td>ID</td>
  54. <td>名称</td>
  55. <td>电话</td>
  56. <td>操作</td>
  57. </tr>
  58. <?php
  59. foreach( $result as $list){
  60. ?>
  61. <tr>
  62. <td><?=$list['id']?></td>
  63. <td><?php echo $list['username'];?></td>
  64. <td><?php echo $list['phone'];?></td>
  65. <td>
  66. <button onclick="location.href='control.php?act=edit&id=<?=$list['id']?>'" >编辑</button>
  67. <button onclick="del(<?=$list['id']?>)">删除</button>
  68. </td>
  69. </tr>
  70. <?php } ?>
  71. </table>
  72. <!-- 分页 -->
  73. <p>
  74. <?php
  75. // 下一页
  76. $next = ($page+1)>=$pages ? $pages : $page+1;
  77. // 上一页
  78. $preve = ($page-1)==0 ? 0 : $page-1;
  79. ?>
  80. <a href="<?php echo $url."?p=1" ?>">首页</a>
  81. <a href="<?php echo $url."?p=".$preve; ?>">上一页</a>
  82. <?php
  83. $html = '';
  84. if($pages>10 && $page>5 && $page<$pages-5){
  85. // 显示2边省略号
  86. $html .= "<a href='".$url."?p=1' class='".$clas."' >1</a>";
  87. $html .= "<a href='".$url."?p=2' class='".$clas."' >2</a>";
  88. $html .="<span>......</span>";
  89. for($i=$page-2;$i<=$page+2;$i++){
  90. $clas = ($page==$i) ? "cla" : null;
  91. $html.="<a href='".$url."?p=".$i."' class='".$clas."' >".$i."</a>";
  92. }
  93. $html .="<span>......</span>";
  94. $html .= "<a href='".$url."?p=".($pages-1)."' class='".$clas."' >".($pages-1)."</a>";
  95. $html .= "<a href='".$url."?p=".$pages."' class='".$clas."' >".$pages."</a>";
  96. }elseif($pages>10 && $page<=5){
  97. // 显示后面省略号
  98. for($i=1;$i<=10;$i++){
  99. $clas = ($page==$i) ? "cla" : null;
  100. $html.="<a href='".$url."?p=".$i."' class='".$clas."' >".$i."</a>";
  101. }
  102. $html .="<span>......</span>";
  103. $html .= "<a href='".$url."?p=".($pages-1)."' class='".$clas."' >".($pages-1)."</a>";
  104. $html .= "<a href='".$url."?p=".$pages."' class='".$clas."' >".$pages."</a>";
  105. }elseif($pages>10 && $page>=$pages-5){
  106. // 显示前面省略号
  107. $html .= "<a href='".$url."?p=1' class='".$clas."' >1</a>";
  108. $html .= "<a href='".$url."?p=2' class='".$clas."' >2</a>";
  109. $html .="<span>......</span>";
  110. for($i=$pages-5;$i<=$pages;$i++){
  111. $clas = ($page==$i) ? "cla" : null;
  112. $html.="<a href='".$url."?p=".$i."' class='".$clas."' >".$i."</a>";
  113. }
  114. }else{
  115. // 显示全部分页
  116. for($i=1;$i<=$pages;$i++){
  117. $html.="<a href='".$url."?p='".$i."' class='".$clas."' >".$i."</a>";
  118. }
  119. }
  120. echo $html;
  121. ?>
  122. <a href="<?php echo $url."?p=".$next; ?>">下一页</a>
  123. <a href="<?php echo $url."?p=".$pages; ?>">尾页</a>
  124. </p>
  125. </body>
  126. <script>
  127. function del(id){
  128. var y= confirm("是否删除");
  129. if(y==true){
  130. location.href="control.php?act=del&id="+id;
  131. }
  132. }
  133. </script>
  134. </html>

3.演示图:

  • 分页

  • 编辑

  • 删除

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