
批改狀態(tài):合格
老師批語:
插入:
//insert方法
public function insert()
{
$sql = 'INSERT ' . $this->table. ' SET ';
$sql .= $this->opts['value'] ?? null;
echo $sql;
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//value方法
public function value(array $value=[]){
$str = '';
if (count($value) > 0) {
foreach ($value as $k => $v) {
$str .= $k . "='" . $v . "',";
}
//去掉最后一個","
$str = substr($str, 0, strlen($str) - 1);
$this->opts['value'] = " $str";
return $this;
}
更新:
//update方法
public function update()
{
$sql = 'UPDATE ' . $this->table. ' SET ';
$sql .= $this->opts['value'] ?? null;
$sql .= $this->opts['where'] ?? null;
echo $sql;
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
刪除:
//delete方法
public function delete()
{
$sql = 'DELETE FROM ' . $this->table;
$sql .= $this->opts['where'] ?? null;
echo $sql;
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
這是學(xué)習(xí)框架底層邏輯最重要的內(nèi)容之一,也是比較難理解的,通過簡單的sql語句拼接實現(xiàn)更新、刪除、插入操作,還沒有使用sql預(yù)處理防注入,有時間再出來吧!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號