欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php 数据库操作mysqli

程序员文章站 2022-05-25 09:09:13
...

学习总结

  • mysqli连接的时候不需要type参数
  • mysqli执行时只需要调用query()方法,返回个结果集
  • 但是如果使用mysqli连接数据库时,如果更换数据库引擎,则所有的方法都得进行更改。

1.数据库连接和增删改查类DBconn.php

  1. <?php
  2. namespace compotents\conn
  3. {
  4. use Exception;
  5. use mysqli;
  6. class DBconn
  7. {
  8. private $config = [];
  9. protected $dbConn;
  10. public function __construct($dbName = 'db_phpstudy',$userName = 'root',$passWord ='root')
  11. {
  12. $this ->config['type'] = 'mysql';
  13. $this ->config['host'] = 'localhost';
  14. $this ->config['dbName'] = $dbName;
  15. $this ->config['userName'] = $userName;
  16. $this ->config['passWord'] = $passWord;
  17. $this ->config['charSet'] = 'utf8';
  18. $this ->config['port'] = '3306';
  19. $this ->connect();
  20. }
  21. public function connect()
  22. {
  23. //拆分数组,键名当做变量名,值当做变量的值,拆分成数据库连接的变量
  24. extract($this->config,EXTR_PREFIX_SAME,'config');
  25. try
  26. {
  27. //1.创建一个mysqli的数据库连接
  28. $this->dbConn = new mysqli($host,$userName,$passWord,$dbName);
  29. //2. 判断是否连接成功?
  30. if ($this->dbConn->connect_errno) echo $this->dbConn->connect_error;
  31. //3.设置数据库连接的字符集
  32. $this->dbConn->set_charset($charSet);
  33. }
  34. catch(Exception $e)
  35. {
  36. die($e->getMessage());
  37. }
  38. }
  39. //查询返回查询结果集
  40. public function select($table,$where)
  41. {
  42. if ($where === '*'):
  43. $sql = "SELECT * FROM `$table`";
  44. else:
  45. $sql = "SELECT * FROM `$table` WHERE $where";
  46. endif;
  47. $info = $this ->dbConn->query($sql);
  48. $records = $info->fetch_all(MYSQLI_ASSOC);
  49. return $records;
  50. }
  51. //插入记录,输出是否成功添加记录
  52. public function insert($table,$insData)
  53. {
  54. //把传入的添加数据的数组转换为一个SQL添加字符串
  55. $insertSet = $this->toSqlStr($insData);
  56. $sql = "INSERT `$table` SET $insertSet";
  57. $flag = $this->dbConn->query($sql);
  58. $rowCount = $this->dbConn->affected_rows;//返回受影响的记录数
  59. if ($flag) {
  60. //$this->dbConn->insert_id返回最后一条插入语句的自增id
  61. if ($rowCount > 0) {
  62. echo '成功添加了 ' . $rowCount . ' 条记录, 新增记录主键ID: ' . $this->dbConn->insert_id;
  63. } else {
  64. echo '没有添加新记录';
  65. }
  66. } else {
  67. //$this->dbConn->errno返回最近函数调用的错误代码
  68. //$this->dbConn->error返回最后一次调用的错误信息
  69. die('添加失败'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
  70. }
  71. }
  72. //更新记录,输出更新几条记录
  73. public function update($table,$data,$where)
  74. {
  75. //把传入的添加数据的数组转换为一个SQL添加字符串
  76. $updateSet = $this->toSqlStr($data);
  77. $sql = "UPDATE `$table` SET $updateSet WHERE $where";
  78. $flag = $this->dbConn->query($sql);
  79. $rowCount = $this->dbConn->affected_rows;//返回受影响的记录数
  80. if ($flag) {
  81. //$this->dbConn->insert_id返回最后一条插入语句的自增id
  82. if ($rowCount > 0) {
  83. echo '成功更新了 ' . $rowCount . ' 条记录';
  84. } else {
  85. echo '没有更新记录';
  86. }
  87. } else {
  88. //$this->dbConn->errno返回最近函数调用的错误代码
  89. //$this->dbConn->error返回最后一次调用的错误信息
  90. die('更新失败'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
  91. }
  92. }
  93. //删除记录,输出是否删除成功
  94. public function delete($table,$where)
  95. {
  96. $sql = "DELETE FROM $table WHERE $where";
  97. $flag = $this->dbConn->query($sql);
  98. $rowCount = $this->dbConn->affected_rows;//返回受影响的记录数
  99. if ($flag) {
  100. //$this->dbConn->insert_id返回最后一条插入语句的自增id
  101. if ($rowCount > 0) {
  102. echo '成功删除了 ' . $rowCount . ' 条记录';
  103. } else {
  104. echo '没有删除记录';
  105. }
  106. } else {
  107. //$this->dbConn->errno返回最近函数调用的错误代码
  108. //$this->dbConn->error返回最后一次调用的错误信息
  109. die('删除失败'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
  110. }
  111. }
  112. public function toSqlStr($arr):string
  113. {
  114. //把数组的键提取到一个数组中
  115. $keys = array_keys($arr);
  116. //把数组的值提取到一个数组中
  117. $value = array_values($arr);
  118. $con = count($keys);
  119. $sqlStr ='';
  120. for ($i=0;$i<$con;$i++):
  121. if($i===$con-1):
  122. $sqlStr .= " `$keys[$i]`='$value[$i]'";
  123. else:
  124. $sqlStr .= " `$keys[$i]`='$value[$i]' ,";
  125. endif;
  126. endfor;
  127. return $sqlStr;
  128. }
  129. }
  130. }
  131. ?>

2.后台首页index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="style/style.css">
  7. <title>商品后台管理</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="row">
  12. <div class="header col-12">
  13. <span>商品后台管理</span>
  14. <div><a href="">返回首页</a> </div>
  15. <div><a href="">退出</a></div>
  16. </div>
  17. </div>
  18. <div class="row">
  19. <div class="aside col-3">
  20. <div><a href="select.php" target="main"><span>查询商品</span> </a></div>
  21. <div><a href="insert.php" target="main"><span>添加商品</span> </a></div>
  22. <div><a href="update.php" target="main"><span>修改商品</span> </a></div>
  23. <div><a href="delete.php" target="main"><span>删除商品</span> </a></div>
  24. </div>
  25. <div class="main col-9">
  26. <iframe src="select.php" frameborder="0" name="main" width="680px" height="510px"></iframe>
  27. </div>
  28. </div>
  29. <div class="row">
  30. <div class="footer col-12">
  31. <div>联系邮箱:<a href="mailto:573661083@qq.com">573661083@qq.com</a></div>
  32. <div>联系电话:<a href="tel:15010046927">1501004xxxx</a></div>
  33. <div>Copyright 1998 - 2020 Tencent. All Rights Reserved</div>
  34. </div>
  35. </div>
  36. </div>
  37. </body>
  38. </html>

3.首页样式表 style.css

  1. @import "reset.css";
  2. /* 整页布局 */
  3. .container {
  4. max-width: 920px;
  5. min-height: 650px;
  6. margin-left: auto;
  7. margin-right: auto;
  8. background-color: white;
  9. display: grid;
  10. gap: 5px;
  11. }
  12. /* 整页中的每行分成12列 */
  13. .container > .row {
  14. display: grid;
  15. grid-template-columns: repeat(12, 1fr);
  16. gap: 5px;
  17. }
  18. /* 头部布局 */
  19. .container > .row > .header {
  20. margin-top: 5px;
  21. background-color: #58c4f2;
  22. max-height: 56px;
  23. border-radius: 3px;
  24. padding: 0px 10px;
  25. display: flex;
  26. flex-flow: row nowrap;
  27. align-items: center;
  28. }
  29. .container > .row > .header > span {
  30. letter-spacing: 2px;
  31. margin-left: 20px;
  32. font-size: 1.5rem;
  33. font-weight: bolder;
  34. }
  35. .container > .row > .header > div > a {
  36. margin: 0px 10px;
  37. }
  38. .container > .row > .header > div > a:hover {
  39. color: lightgreen;
  40. }
  41. .container > .row > .header > div:nth-of-type(1) {
  42. margin-left: auto;
  43. }
  44. /* 侧边栏 */
  45. .container > .row > .aside {
  46. min-height: 500px;
  47. background-color: #ccc;
  48. border-radius: 3px;
  49. display: flex;
  50. flex-flow: column nowrap;
  51. padding: 5px;
  52. }
  53. /* 侧边栏导航 */
  54. .container > .row > .aside > div {
  55. background-color: #58c4f2;
  56. height: 40px;
  57. margin: 2px 8px;
  58. border-radius: 10px;
  59. }
  60. .container > .row > .aside > div > a {
  61. width: 100%;
  62. height: 100%;
  63. display: flex;
  64. justify-content: center;
  65. align-items: center;
  66. }
  67. .container > .row > .aside > div > a > span {
  68. font-size: 1.2rem;
  69. letter-spacing: 2px;
  70. }
  71. .container > .row > .aside > div:hover {
  72. background-color: lightgreen;
  73. box-shadow: 0 0 5px #555;
  74. }
  75. /* 主体内容显示区 */
  76. .container > .row > .main {
  77. min-height: 500px;
  78. }
  79. /* 页脚 */
  80. .container > .row > .footer {
  81. max-height: 80px;
  82. margin-bottom: 5px;
  83. background-color: #58c4f2;
  84. border-radius: 3px;
  85. display: flex;
  86. flex-flow: column nowrap;
  87. justify-content: center;
  88. align-items: center;
  89. }
  90. .container > .row > .footer > div > a:hover {
  91. color: lightgreen;
  92. }
  93. /* 12列栅格布局 */
  94. .col-1 {
  95. grid-column-end: span 1;
  96. }
  97. .col-2 {
  98. grid-column-end: span 2;
  99. }
  100. .col-3 {
  101. grid-column-end: span 3;
  102. }
  103. .col-4 {
  104. grid-column-end: span 4;
  105. }
  106. .col-5 {
  107. grid-column-end: span 5;
  108. }
  109. .col-6 {
  110. grid-column-end: span 6;
  111. }
  112. .col-7 {
  113. grid-column-end: span 7;
  114. }
  115. .col-8 {
  116. grid-column-end: span 8;
  117. }
  118. .col-9 {
  119. grid-column-end: span 9;
  120. }
  121. .col-10 {
  122. grid-column-end: span 10;
  123. }
  124. .col-11 {
  125. grid-column-end: span 11;
  126. }
  127. .col-12 {
  128. grid-column-end: span 12;
  129. }

4.前端综合处理handle.php

  1. <?php
  2. require 'autoLoad.php';
  3. use compotents\conn\DBconn;
  4. $user =new DBconn();
  5. $table = 'tb_goods';//表名
  6. $where =''; //判断的条件
  7. $data =[];//添加或者更新的数据
  8. $action = $_GET['action'];
  9. switch ($action)
  10. {
  11. case 'insert':
  12. $name = $_POST['goodsName'];
  13. $price = $_POST['goodsPrice'];
  14. $unit = $_POST['goodsUnit'];
  15. $date = $_POST['goodsSdate'];
  16. $data = ['name'=>"$name",'price'=>"$price",'unit'=>"$unit",'sdate'=>"$date"];
  17. $user->insert($table,$data);
  18. break;
  19. case 'update':
  20. $id = $_GET['id'];
  21. $name = $_POST['goodsName'];
  22. $price = $_POST['goodsPrice'];
  23. $unit = $_POST['goodsUnit'];
  24. $sdate = $_POST['goodSdate'];
  25. $where = "`id`=$id";
  26. $data = ['name'=>"$name",'price'=>"$price",'unit'=>"$unit",'sdate'=>"$sdate"];
  27. $user->update($table,$data,$where);
  28. break;
  29. case 'delete':
  30. $id = $_GET['id'];
  31. $where = "`id`=$id";
  32. $user->delete($table,$where);
  33. break;
  34. default:
  35. echo '不支持此操作';
  36. }
  37. ?>

5.查询商品select.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="style/select.css">
  7. <title>商品查询页</title>
  8. </head>
  9. <body>
  10. <div class="show">
  11. <div class="row">
  12. <div>商品编号</div>
  13. <div>商品名称</div>
  14. <div>商品价格</div>
  15. <div>上架时间</div>
  16. </div>
  17. <?php
  18. require 'autoLoad.php';
  19. use compotents\conn\DBconn;
  20. $user =new DBconn();
  21. $table = 'tb_goods';//表名
  22. $where ='*'; //判断的条件 如果选择所有数据则为*
  23. //显示所有用户信息
  24. $records = $user->select($table,$where);
  25. foreach($records as $res):
  26. ?>
  27. <div class="row">
  28. <div><?php echo $res['id']; ?></div>
  29. <div><?php echo $res['name']; ?></div>
  30. <div><?php echo $res['price'],'元/',$res['unit']; ?></div>
  31. <div><?php echo $res['sdate']; ?></div>
  32. </div>
  33. <?php
  34. endforeach;
  35. ?>
  36. </div>
  37. </body>
  38. </html>
  • 查询效果图
    php 数据库操作mysqli

5.添加商品insert.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <link rel="stylesheet" href="style/insert.css" />
  7. <title>商品添加页</title>
  8. </head>
  9. <body>
  10. <div class="add">
  11. <h2>商品添加</h2>
  12. <form action="handle.php?action=insert" method="POST">
  13. <div>
  14. <label for="goodsName">商品名称:</label>
  15. <input type="text" name="goodsName" placeholder="商品名称不能为空" require />
  16. </div>
  17. <div>
  18. <label for="goodsPrice">商品单价:</label>
  19. <input type="text" name="goodsPrice" placeholder="输入商品价格" />
  20. </div>
  21. <div>
  22. <label for="goodsUnit">商品单位:</label>
  23. <select name="goodsUnit" id="goodsUnit">
  24. <option value="斤" selected></option>
  25. <option value="盒"></option>
  26. <option value="袋"></option>
  27. <option value="捆"></option>
  28. <option value="筐"></option>
  29. <option value="箱"></option>
  30. <option value="桶"></option>
  31. </select>
  32. </div>
  33. <div>
  34. <label for="goodsSdate">上架时间:</label>
  35. <input type="date" name="goodsSdate" id="goodsSdate" value="<?php echo date('Y-m-d'); ?>" />
  36. </div>
  37. <div>
  38. <button type="submit">添加</button>
  39. </div>
  40. </form>
  41. </div>
  42. </body>
  43. </html>
  • 添加商品
    php 数据库操作mysqli

  • 添加成功
    php 数据库操作mysqli

5.修改商品update.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="style/select.css">
  7. <title>商品修改页</title>
  8. </head>
  9. <body>
  10. <div class="show">
  11. <div class="row">
  12. <div>商品编号</div>
  13. <div>商品名称</div>
  14. <div>价格/单位</div>
  15. <div>上架时间</div>
  16. <div>更新操作</div>
  17. </div>
  18. <?php
  19. require 'autoLoad.php';
  20. use compotents\conn\DBconn;
  21. $user =new DBconn();
  22. $table = 'tb_goods';//表名
  23. $where ='*'; //判断的条件 如果选择所有数据则为*
  24. //显示所有用户信息
  25. $records = $user->select($table,$where);
  26. foreach($records as $res):
  27. ?>
  28. <div class="row">
  29. <form action="handle.php?action=update&id=<?php echo $res['id']; ?>" method="POST">
  30. <div style="width:100px"><?php echo $res['id']; ?></div>
  31. <input type="text" name="goodsName" id="goodsName" style="width:120px"
  32. value="<?php echo $res['name']; ?>">
  33. <div>
  34. <input type="text" name="goodsPrice" id="goodsPrice" style="width:50px"
  35. value="<?php echo $res['price']; ?>">/
  36. <input type="text" name="goodsUnit" id="goodsUnit" style="width:50px"
  37. value="<?php echo $res['unit']; ?>">
  38. </div>
  39. <input type="date" name="goodSdate" id="goodSdate" style="width:120px"
  40. value="<?php echo $res['sdate']; ?>">
  41. <div style="width:120px"><button type="submit">修改</button></div>
  42. </form>
  43. </div>
  44. <?php
  45. endforeach;
  46. ?>
  47. </div>
  48. </body>
  49. </html>
  • 修改商品
    php 数据库操作mysqli

  • 修改成功
    php 数据库操作mysqli

5.删除商品delete.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="style/select.css">
  7. <title>商品删除页</title>
  8. </head>
  9. <body>
  10. <div class="show">
  11. <div class="row">
  12. <div>商品编号</div>
  13. <div>商品名称</div>
  14. <div>商品价格</div>
  15. <div>上架时间</div>
  16. <div>删除操作</div>
  17. </div>
  18. <?php
  19. require 'autoLoad.php';
  20. use compotents\conn\DBconn;
  21. $user =new DBconn();
  22. $table = 'tb_goods';//表名
  23. $where ='*'; //判断的条件 如果选择所有数据则为*
  24. //显示所有用户信息
  25. $records = $user->select($table,$where);
  26. foreach($records as $res):
  27. ?>
  28. <div class="row">
  29. <div><?php echo $res['id']; ?></div>
  30. <div><?php echo $res['name']; ?></div>
  31. <div><?php echo $res['price'],'元/',$res['unit']; ?></div>
  32. <div><?php echo $res['sdate']; ?></div>
  33. <div><a href="handle.php?action=delete&id=<?php echo $res['id']; ?>">删除</a></div>
  34. </div>
  35. <?php
  36. endforeach;
  37. ?>
  38. </div>
  39. </body>
  40. </html>
  • 删除商品
    php 数据库操作mysqli

  • 删除成功
    php 数据库操作mysqli