亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 64
粉絲 6
評論 2
訪問量 100652
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
php 數(shù)據(jù)庫操作mysqli
王嬌
原創(chuàng)
1587人瀏覽過

學(xué)習(xí)總結(jié)

  • mysqli連接的時候不需要type參數(shù)
  • mysqli執(zhí)行時只需要調(diào)用query()方法,返回個結(jié)果集
  • 但是如果使用mysqli連接數(shù)據(jù)庫時,如果更換數(shù)據(jù)庫引擎,則所有的方法都得進(jìn)行更改。

1.數(shù)據(jù)庫連接和增刪改查類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. //拆分?jǐn)?shù)組,鍵名當(dāng)做變量名,值當(dāng)做變量的值,拆分成數(shù)據(jù)庫連接的變量
  24. extract($this->config,EXTR_PREFIX_SAME,'config');
  25. try
  26. {
  27. //1.創(chuàng)建一個mysqli的數(shù)據(jù)庫連接
  28. $this->dbConn = new mysqli($host,$userName,$passWord,$dbName);
  29. //2. 判斷是否連接成功?
  30. if ($this->dbConn->connect_errno) echo $this->dbConn->connect_error;
  31. //3.設(shè)置數(shù)據(jù)庫連接的字符集
  32. $this->dbConn->set_charset($charSet);
  33. }
  34. catch(Exception $e)
  35. {
  36. die($e->getMessage());
  37. }
  38. }
  39. //查詢返回查詢結(jié)果集
  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. //把傳入的添加數(shù)據(jù)的數(shù)組轉(zhuǎn)換為一個SQL添加字符串
  55. $insertSet = $this->toSqlStr($insData);
  56. $sql = "INSERT `$table` SET $insertSet";
  57. $flag = $this->dbConn->query($sql);
  58. $rowCount = $this->dbConn->affected_rows;//返回受影響的記錄數(shù)
  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返回最近函數(shù)調(diào)用的錯誤代碼
  68. //$this->dbConn->error返回最后一次調(diào)用的錯誤信息
  69. die('添加失敗'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
  70. }
  71. }
  72. //更新記錄,輸出更新幾條記錄
  73. public function update($table,$data,$where)
  74. {
  75. //把傳入的添加數(shù)據(jù)的數(shù)組轉(zhuǎn)換為一個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;//返回受影響的記錄數(shù)
  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返回最近函數(shù)調(diào)用的錯誤代碼
  89. //$this->dbConn->error返回最后一次調(diào)用的錯誤信息
  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;//返回受影響的記錄數(shù)
  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返回最近函數(shù)調(diào)用的錯誤代碼
  108. //$this->dbConn->error返回最后一次調(diào)用的錯誤信息
  109. die('刪除失敗'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
  110. }
  111. }
  112. public function toSqlStr($arr):string
  113. {
  114. //把數(shù)組的鍵提取到一個數(shù)組中
  115. $keys = array_keys($arr);
  116. //把數(shù)組的值提取到一個數(shù)組中
  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>聯(lián)系郵箱:<a href="mailto:573661083@qq.com">573661083@qq.com</a></div>
  32. <div>聯(lián)系電話:<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. /* 側(cè)邊欄 */
  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. /* 側(cè)邊欄導(dǎo)航 */
  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. /* 主體內(nèi)容顯示區(qū) */
  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 =[];//添加或者更新的數(shù)據(jù)
  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>商品價(jià)格</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 ='*'; //判斷的條件 如果選擇所有數(shù)據(jù)則為*
  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>
  • 查詢效果圖

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">商品單價(jià):</label>
  19. <input type="text" name="goodsPrice" placeholder="輸入商品價(jià)格" />
  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>
  • 添加商品

  • 添加成功

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>價(jià)格/單位</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 ='*'; //判斷的條件 如果選擇所有數(shù)據(jù)則為*
  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>
  • 修改商品

  • 修改成功

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>商品價(jià)格</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 ='*'; //判斷的條件 如果選擇所有數(shù)據(jù)則為*
  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>
  • 刪除商品

  • 刪除成功

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語:不錯 就差一個分頁了
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)