博客列表 >数据库功能:添加列表功能

数据库功能:添加列表功能

手机用户1617360551
手机用户1617360551原创
2021年10月20日 14:52:41729浏览

数据库功能:

1.连接数据库,并将数据输出到列表页

代码

  1. <?php
  2. // 1.bindColmun 将结果绑定到指定变量上
  3. // try {
  4. // $pdo = new PDO('mysql:host=localhost;dbname=user','user','user123');
  5. // } catch (PDOException $e) {
  6. // echo '数据链接失败:'. $e->getMessage();
  7. // }
  8. // $pre = $pdo->prepare('SELECT * FROM `user`');
  9. // $exe = $pre->execute();
  10. // $pre->bindColumn('name',$name);
  11. // while($pre->fetch(PDO::FETCH_ASSOC)){
  12. // echo '姓名:'.$name;
  13. // echo '<br>';
  14. // }
  15. // 2.编码
  16. // header('content-type:text/html;charset=utf8');
  17. // new PDO('mysql:host=localhost;dbname=user','user','user123',
  18. // array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
  19. try {
  20. $pdo = new PDO('mysql:host=localhost;dbname=user','user','user123');
  21. } catch (PDOException $e) {
  22. echo '数据库链接失败'.$e->getMessage();
  23. }
  24. $pre = $pdo->prepare('SELECT * FROM `user`');
  25. $exe = $pre->execute();
  26. // var_dump($exe);
  27. $data = $pre->fetchAll();
  28. // print_r($data);
  29. // $pdo->query('SET NAMES utf8');
  30. // $pdo=null;
  31. // echo time();
  32. ?>
  33. <!-- // 3.实例 -->
  34. <!DOCTYPE html>
  35. <html lang="en">
  36. <head>
  37. <meta charset="UTF-8">
  38. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  39. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  40. <title>表格</title>
  41. </head>
  42. <body>
  43. <a href="add.php">添加</a>
  44. <table border="1">
  45. <thead>
  46. <tr>
  47. <th width='50px'>id</th>
  48. <th width='100px'>姓名</th>
  49. <th width='120px' >手机号</th>
  50. <th width='200px' >入职时间</th>
  51. </tr>
  52. </thead>
  53. <tbody>
  54. <?php
  55. foreach($data as $v){
  56. ?>
  57. <tr>
  58. <td><?= $v['id'] ?></td>
  59. <td><?= $v['name'] ?></td>
  60. <td><?= $v['phone'] ?></td>
  61. <td><?= $v['add_time'] ?></td>
  62. </tr>
  63. <?php
  64. }
  65. ?>
  66. </tbody>
  67. </table>
  68. </body>
  69. </html>

2.数据库添加功能

代码

  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. * {
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. body {
  15. background-color: #888;
  16. }
  17. .add {
  18. position: relative;
  19. top: 50px;
  20. left: 50px;
  21. border-radius: 5px;
  22. padding: 20px;
  23. display: flex;
  24. flex-flow: column;
  25. justify-content: space-between;
  26. width: 300px;
  27. height: 180px;
  28. gap: 5px;
  29. background-color: lightskyblue;
  30. box-shadow: 4px 4px 4px #666;
  31. color: white;
  32. align-items: center;
  33. }
  34. .submit {
  35. margin-top: 15px;
  36. background-color: lightyellow;
  37. width: 50px;
  38. }
  39. h2 {
  40. color: slateblue;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <form action="" class="add" method="post">
  46. <h2>添加用户</h2>
  47. <div>
  48. <label for="id"> 用户ID: </label>
  49. <input type="number" name="id" id="id" />
  50. </div>
  51. <div>
  52. <label for="name">用户名:</label>
  53. <input type="text" name="name" id="name"/>
  54. </div>
  55. <div>
  56. <label for="phone">手机号: </label>
  57. <input type="number" name="phone" id="phone" />
  58. </div>
  59. <input type="submit" class="submit" />
  60. </form>
  61. </body>
  62. </html>
  63. <?php
  64. // print_r($_POST);
  65. if(!empty($_POST)){
  66. if(empty($_POST['id'])){
  67. echo '<div style="color:red;margin-top:100px">id不能为空</div>';
  68. exit;
  69. }
  70. if(empty($_POST['name'])){
  71. echo '<div style="color:red;margin-top:100px">用户名不能为空</div>';
  72. }
  73. $pdo = new PDO('mysql:host=localhost;dbname=user','user','user123');
  74. //查询数据
  75. // $pre = $pdo->prepare('SELECT * FROM `user`');
  76. // $pre->execute();
  77. // $data = $pre->fetchAll();
  78. // print_r($data);
  79. //插入数据
  80. //组装sql语句
  81. $sql = 'INSERT INTO `user` SET `id` = ' . $_POST['id'] . ",`name` ='" .$_POST['name']. "'";
  82. //判断手机号是否存在,如果有就添加sql语句
  83. if(!empty($_POST['phone'])){
  84. $sql .= ',`phone`='.$_POST['phone'];
  85. }
  86. $pdo->prepare($sql);
  87. echo '<div style="color:red;margin-top:100px">'.$sql.'</div>';
  88. //预处理
  89. $pre = $pdo->prepare($sql);
  90. $pre->execute();
  91. var_dump($pre->rowCount());
  92. if($pre->rowCount() > 0){
  93. echo '<script>alert("添加成功");window.location.href="list.php"</script>';
  94. }else{
  95. echo '<script>alert("添加失败,请重试!!!")</script>';
  96. }
  97. }
  98. // echo $_POST['name'];

数据库添加用户页面

" class="reference-link">添加用户失败,

添加用户成功返回列表页

sql语句

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