
批改狀態(tài):合格
老師批語:不錯 就差一個分頁了
<?php
namespace compotents\conn
{
use Exception;
use mysqli;
class DBconn
{
private $config = [];
protected $dbConn;
public function __construct($dbName = 'db_phpstudy',$userName = 'root',$passWord ='root')
{
$this ->config['type'] = 'mysql';
$this ->config['host'] = 'localhost';
$this ->config['dbName'] = $dbName;
$this ->config['userName'] = $userName;
$this ->config['passWord'] = $passWord;
$this ->config['charSet'] = 'utf8';
$this ->config['port'] = '3306';
$this ->connect();
}
public function connect()
{
//拆分?jǐn)?shù)組,鍵名當(dāng)做變量名,值當(dāng)做變量的值,拆分成數(shù)據(jù)庫連接的變量
extract($this->config,EXTR_PREFIX_SAME,'config');
try
{
//1.創(chuàng)建一個mysqli的數(shù)據(jù)庫連接
$this->dbConn = new mysqli($host,$userName,$passWord,$dbName);
//2. 判斷是否連接成功?
if ($this->dbConn->connect_errno) echo $this->dbConn->connect_error;
//3.設(shè)置數(shù)據(jù)庫連接的字符集
$this->dbConn->set_charset($charSet);
}
catch(Exception $e)
{
die($e->getMessage());
}
}
//查詢返回查詢結(jié)果集
public function select($table,$where)
{
if ($where === '*'):
$sql = "SELECT * FROM `$table`";
else:
$sql = "SELECT * FROM `$table` WHERE $where";
endif;
$info = $this ->dbConn->query($sql);
$records = $info->fetch_all(MYSQLI_ASSOC);
return $records;
}
//插入記錄,輸出是否成功添加記錄
public function insert($table,$insData)
{
//把傳入的添加數(shù)據(jù)的數(shù)組轉(zhuǎn)換為一個SQL添加字符串
$insertSet = $this->toSqlStr($insData);
$sql = "INSERT `$table` SET $insertSet";
$flag = $this->dbConn->query($sql);
$rowCount = $this->dbConn->affected_rows;//返回受影響的記錄數(shù)
if ($flag) {
//$this->dbConn->insert_id返回最后一條插入語句的自增id
if ($rowCount > 0) {
echo '成功添加了 ' . $rowCount . ' 條記錄, 新增記錄主鍵ID: ' . $this->dbConn->insert_id;
} else {
echo '沒有添加新記錄';
}
} else {
//$this->dbConn->errno返回最近函數(shù)調(diào)用的錯誤代碼
//$this->dbConn->error返回最后一次調(diào)用的錯誤信息
die('添加失敗'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
}
}
//更新記錄,輸出更新幾條記錄
public function update($table,$data,$where)
{
//把傳入的添加數(shù)據(jù)的數(shù)組轉(zhuǎn)換為一個SQL添加字符串
$updateSet = $this->toSqlStr($data);
$sql = "UPDATE `$table` SET $updateSet WHERE $where";
$flag = $this->dbConn->query($sql);
$rowCount = $this->dbConn->affected_rows;//返回受影響的記錄數(shù)
if ($flag) {
//$this->dbConn->insert_id返回最后一條插入語句的自增id
if ($rowCount > 0) {
echo '成功更新了 ' . $rowCount . ' 條記錄';
} else {
echo '沒有更新記錄';
}
} else {
//$this->dbConn->errno返回最近函數(shù)調(diào)用的錯誤代碼
//$this->dbConn->error返回最后一次調(diào)用的錯誤信息
die('更新失敗'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
}
}
//刪除記錄,輸出是否刪除成功
public function delete($table,$where)
{
$sql = "DELETE FROM $table WHERE $where";
$flag = $this->dbConn->query($sql);
$rowCount = $this->dbConn->affected_rows;//返回受影響的記錄數(shù)
if ($flag) {
//$this->dbConn->insert_id返回最后一條插入語句的自增id
if ($rowCount > 0) {
echo '成功刪除了 ' . $rowCount . ' 條記錄';
} else {
echo '沒有刪除記錄';
}
} else {
//$this->dbConn->errno返回最近函數(shù)調(diào)用的錯誤代碼
//$this->dbConn->error返回最后一次調(diào)用的錯誤信息
die('刪除失敗'. $this->dbConn->errno . ' : ' . $this->dbConn->error);
}
}
public function toSqlStr($arr):string
{
//把數(shù)組的鍵提取到一個數(shù)組中
$keys = array_keys($arr);
//把數(shù)組的值提取到一個數(shù)組中
$value = array_values($arr);
$con = count($keys);
$sqlStr ='';
for ($i=0;$i<$con;$i++):
if($i===$con-1):
$sqlStr .= " `$keys[$i]`='$value[$i]'";
else:
$sqlStr .= " `$keys[$i]`='$value[$i]' ,";
endif;
endfor;
return $sqlStr;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style/style.css">
<title>商品后臺管理</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="header col-12">
<span>商品后臺管理</span>
<div><a href="">返回首頁</a> </div>
<div><a href="">退出</a></div>
</div>
</div>
<div class="row">
<div class="aside col-3">
<div><a href="select.php" target="main"><span>查詢商品</span> </a></div>
<div><a href="insert.php" target="main"><span>添加商品</span> </a></div>
<div><a href="update.php" target="main"><span>修改商品</span> </a></div>
<div><a href="delete.php" target="main"><span>刪除商品</span> </a></div>
</div>
<div class="main col-9">
<iframe src="select.php" frameborder="0" name="main" width="680px" height="510px"></iframe>
</div>
</div>
<div class="row">
<div class="footer col-12">
<div>聯(lián)系郵箱:<a href="mailto:573661083@qq.com">573661083@qq.com</a></div>
<div>聯(lián)系電話:<a href="tel:15010046927">1501004xxxx</a></div>
<div>Copyright 1998 - 2020 Tencent. All Rights Reserved</div>
</div>
</div>
</div>
</body>
</html>
@import "reset.css";
/* 整頁布局 */
.container {
max-width: 920px;
min-height: 650px;
margin-left: auto;
margin-right: auto;
background-color: white;
display: grid;
gap: 5px;
}
/* 整頁中的每行分成12列 */
.container > .row {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 5px;
}
/* 頭部布局 */
.container > .row > .header {
margin-top: 5px;
background-color: #58c4f2;
max-height: 56px;
border-radius: 3px;
padding: 0px 10px;
display: flex;
flex-flow: row nowrap;
align-items: center;
}
.container > .row > .header > span {
letter-spacing: 2px;
margin-left: 20px;
font-size: 1.5rem;
font-weight: bolder;
}
.container > .row > .header > div > a {
margin: 0px 10px;
}
.container > .row > .header > div > a:hover {
color: lightgreen;
}
.container > .row > .header > div:nth-of-type(1) {
margin-left: auto;
}
/* 側(cè)邊欄 */
.container > .row > .aside {
min-height: 500px;
background-color: #ccc;
border-radius: 3px;
display: flex;
flex-flow: column nowrap;
padding: 5px;
}
/* 側(cè)邊欄導(dǎo)航 */
.container > .row > .aside > div {
background-color: #58c4f2;
height: 40px;
margin: 2px 8px;
border-radius: 10px;
}
.container > .row > .aside > div > a {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container > .row > .aside > div > a > span {
font-size: 1.2rem;
letter-spacing: 2px;
}
.container > .row > .aside > div:hover {
background-color: lightgreen;
box-shadow: 0 0 5px #555;
}
/* 主體內(nèi)容顯示區(qū) */
.container > .row > .main {
min-height: 500px;
}
/* 頁腳 */
.container > .row > .footer {
max-height: 80px;
margin-bottom: 5px;
background-color: #58c4f2;
border-radius: 3px;
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
}
.container > .row > .footer > div > a:hover {
color: lightgreen;
}
/* 12列柵格布局 */
.col-1 {
grid-column-end: span 1;
}
.col-2 {
grid-column-end: span 2;
}
.col-3 {
grid-column-end: span 3;
}
.col-4 {
grid-column-end: span 4;
}
.col-5 {
grid-column-end: span 5;
}
.col-6 {
grid-column-end: span 6;
}
.col-7 {
grid-column-end: span 7;
}
.col-8 {
grid-column-end: span 8;
}
.col-9 {
grid-column-end: span 9;
}
.col-10 {
grid-column-end: span 10;
}
.col-11 {
grid-column-end: span 11;
}
.col-12 {
grid-column-end: span 12;
}
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
$user =new DBconn();
$table = 'tb_goods';//表名
$where =''; //判斷的條件
$data =[];//添加或者更新的數(shù)據(jù)
$action = $_GET['action'];
switch ($action)
{
case 'insert':
$name = $_POST['goodsName'];
$price = $_POST['goodsPrice'];
$unit = $_POST['goodsUnit'];
$date = $_POST['goodsSdate'];
$data = ['name'=>"$name",'price'=>"$price",'unit'=>"$unit",'sdate'=>"$date"];
$user->insert($table,$data);
break;
case 'update':
$id = $_GET['id'];
$name = $_POST['goodsName'];
$price = $_POST['goodsPrice'];
$unit = $_POST['goodsUnit'];
$sdate = $_POST['goodSdate'];
$where = "`id`=$id";
$data = ['name'=>"$name",'price'=>"$price",'unit'=>"$unit",'sdate'=>"$sdate"];
$user->update($table,$data,$where);
break;
case 'delete':
$id = $_GET['id'];
$where = "`id`=$id";
$user->delete($table,$where);
break;
default:
echo '不支持此操作';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style/select.css">
<title>商品查詢頁</title>
</head>
<body>
<div class="show">
<div class="row">
<div>商品編號</div>
<div>商品名稱</div>
<div>商品價(jià)格</div>
<div>上架時間</div>
</div>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
$user =new DBconn();
$table = 'tb_goods';//表名
$where ='*'; //判斷的條件 如果選擇所有數(shù)據(jù)則為*
//顯示所有用戶信息
$records = $user->select($table,$where);
foreach($records as $res):
?>
<div class="row">
<div><?php echo $res['id']; ?></div>
<div><?php echo $res['name']; ?></div>
<div><?php echo $res['price'],'元/',$res['unit']; ?></div>
<div><?php echo $res['sdate']; ?></div>
</div>
<?php
endforeach;
?>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style/insert.css" />
<title>商品添加頁</title>
</head>
<body>
<div class="add">
<h2>商品添加</h2>
<form action="handle.php?action=insert" method="POST">
<div>
<label for="goodsName">商品名稱:</label>
<input type="text" name="goodsName" placeholder="商品名稱不能為空" require />
</div>
<div>
<label for="goodsPrice">商品單價(jià):</label>
<input type="text" name="goodsPrice" placeholder="輸入商品價(jià)格" />
</div>
<div>
<label for="goodsUnit">商品單位:</label>
<select name="goodsUnit" id="goodsUnit">
<option value="斤" selected>斤</option>
<option value="盒">盒</option>
<option value="袋">袋</option>
<option value="捆">捆</option>
<option value="筐">筐</option>
<option value="箱">箱</option>
<option value="桶">桶</option>
</select>
</div>
<div>
<label for="goodsSdate">上架時間:</label>
<input type="date" name="goodsSdate" id="goodsSdate" value="<?php echo date('Y-m-d'); ?>" />
</div>
<div>
<button type="submit">添加</button>
</div>
</form>
</div>
</body>
</html>
添加商品
添加成功
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style/select.css">
<title>商品修改頁</title>
</head>
<body>
<div class="show">
<div class="row">
<div>商品編號</div>
<div>商品名稱</div>
<div>價(jià)格/單位</div>
<div>上架時間</div>
<div>更新操作</div>
</div>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
$user =new DBconn();
$table = 'tb_goods';//表名
$where ='*'; //判斷的條件 如果選擇所有數(shù)據(jù)則為*
//顯示所有用戶信息
$records = $user->select($table,$where);
foreach($records as $res):
?>
<div class="row">
<form action="handle.php?action=update&id=<?php echo $res['id']; ?>" method="POST">
<div style="width:100px"><?php echo $res['id']; ?></div>
<input type="text" name="goodsName" id="goodsName" style="width:120px"
value="<?php echo $res['name']; ?>">
<div>
<input type="text" name="goodsPrice" id="goodsPrice" style="width:50px"
value="<?php echo $res['price']; ?>">/
<input type="text" name="goodsUnit" id="goodsUnit" style="width:50px"
value="<?php echo $res['unit']; ?>">
</div>
<input type="date" name="goodSdate" id="goodSdate" style="width:120px"
value="<?php echo $res['sdate']; ?>">
<div style="width:120px"><button type="submit">修改</button></div>
</form>
</div>
<?php
endforeach;
?>
</div>
</body>
</html>
修改商品
修改成功
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style/select.css">
<title>商品刪除頁</title>
</head>
<body>
<div class="show">
<div class="row">
<div>商品編號</div>
<div>商品名稱</div>
<div>商品價(jià)格</div>
<div>上架時間</div>
<div>刪除操作</div>
</div>
<?php
require 'autoLoad.php';
use compotents\conn\DBconn;
$user =new DBconn();
$table = 'tb_goods';//表名
$where ='*'; //判斷的條件 如果選擇所有數(shù)據(jù)則為*
//顯示所有用戶信息
$records = $user->select($table,$where);
foreach($records as $res):
?>
<div class="row">
<div><?php echo $res['id']; ?></div>
<div><?php echo $res['name']; ?></div>
<div><?php echo $res['price'],'元/',$res['unit']; ?></div>
<div><?php echo $res['sdate']; ?></div>
<div><a href="handle.php?action=delete&id=<?php echo $res['id']; ?>">刪除</a></div>
</div>
<?php
endforeach;
?>
</div>
</body>
</html>
刪除商品
刪除成功
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號