摘要:發(fā)現(xiàn)忘記的差不多了,也不會寫了想著寫點(diǎn)什么東西簡單的寫個購物車類請先登錄添加商品到購物車修改購買數(shù)量清空購物車從購物車中刪除商品處理數(shù)據(jù)簡單使用添加到購物車刪除指定的商品清空
發(fā)現(xiàn)pdo忘記的差不多了,sql也不會寫了 想著寫點(diǎn)什么東西 簡單的寫個購物車類
pdo = new PDO("mysql:host=$host;dbname=$db", "$user", "$pwd", array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); $this->pdo->query("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } //添加商品到購物車 public function add_cart($productid, $num) { $sql = "select price from shop_product where id=?"; $stmt = $this->pdo->prepare($sql); $stmt->execute(array($productid)); $data = $stmt->fetch(PDO::FETCH_ASSOC); $price = $data["price"]; $createtime = time(); $sql = "select * from shop_cart where productid=? and userid=?"; $stmt = $this->pdo->prepare($sql); $stmt->execute(array($productid, $_SESSION["user_id"])); $data = $stmt->fetch(PDO::FETCH_ASSOC); if ($data) { $sql = "update shop_cart set num=num+? where userid=? and productid=?"; $params = array($num, $_SESSION["user_id"], $productid); } else { $sql = "insert into shop_cart(productid,num,userid,price,createtime) values(?,?,?,?,?)"; $params = array($productid, $num, $_SESSION["user_id"], $price, $createtime); } $stmt = $this->pdo->prepare($sql); $stmt->execute($params); $rows = $stmt->rowCount(); return $rows ? show(1, "ok", $rows) : show(0, "fail"); } //修改購買數(shù)量 public function change_num($productid, $num) { $sql = "update shop_cart set num=? where userid=? and productid=?"; $stmt = $this->pdo->prepare($sql); $stmt->execute(array($num, $_SESSION["user_id"], $productid)); $rows = $stmt->rowCount(); return $rows ? show(1, "ok", $rows) : show(0, "fail"); } //清空購物車 public function clear_cart() { $sql = "delete from shop_cart where userid=?"; $stmt = $this->pdo->prepare($sql); $this->pdo->execute(array($this->user_id)); $rows = $stmt->rowCount(); return $rows ? show(1, "ok", $rows) : show(0, "fail"); } //從購物車中刪除商品 public function remove_cart($productid) { $sql = "delete from shop_cart where productid=? and userid=?"; $stmt = $this->pdo->prepare($sql); $stmt->execute(array($productid, $_SESSION["user_id"])); $rows = $stmt->rowCount(); return $rows ? show(1, "ok", $rows) : show(0, "fail"); } } //處理數(shù)據(jù) function show($status, $message, $data = array()) { $result = array( "status" => $status, "message" => $message, "data" => $data ); exit(json_encode($result)); } //簡單使用 $user = [ "host" => "", "user" => "root", "pwd" => "root", "db" => "shop", ]; $productid = intval($_POST["productid"]); $num = intval($_POST["num"]); $cart = new Cart($user); //添加到購物車 $cart->add_cart($productid, $num); //刪除指定的商品 $cart->remove_cart($productid); //清空 $cart->clear_cart(); ?>
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/23148.html
摘要:操作數(shù)據(jù)庫的種形式使用擴(kuò)展類庫推薦使用擴(kuò)展類庫這是類庫的升級版,但已經(jīng)不推薦使用擴(kuò)展包含哪三個類與區(qū)別可以支持多種數(shù)據(jù)庫,而且操作方法一致只支持?jǐn)?shù)據(jù)庫如何使用連接數(shù)據(jù)庫什么是如何關(guān)閉連接通過來連接數(shù)據(jù)庫,其中必須傳入數(shù)據(jù)源名稱數(shù)據(jù)源名稱是 PHP操作數(shù)據(jù)庫的2種形式 使用 PDO 擴(kuò)展類庫(推薦) 使用 Mysqli 擴(kuò)展類庫(這是Mysql類庫的升級版,但已經(jīng)不推薦使用) PDO...
摘要:注在常駐內(nèi)存單例模式下,這種多次用一個類進(jìn)行查詢的情形很常見。斷線重連對于典型環(huán)境而言,一次的查詢已經(jīng)隨著的請求而結(jié)束,的垃圾回收功能會回收一次請求周期內(nèi)的數(shù)據(jù)。但在常駐內(nèi)存的環(huán)境下,尤其是單例模式下,數(shù)據(jù)庫驅(qū)動類可能一直在內(nèi)存中不被銷毀。 構(gòu)造、執(zhí)行第一條語句 上一篇完成了代碼結(jié)構(gòu)的搭建和 PDO 的基礎(chǔ)封裝,這一篇我們來講如何構(gòu)造一個最基本的 SQL 語句,并執(zhí)行得到結(jié)果。 que...
摘要:同樣的,添加屬性,修改函數(shù)構(gòu)造語句的方法基類中添加方法檢測有沒有設(shè)置子句構(gòu)建語句參數(shù)綁定返回影響的行數(shù)更新數(shù)據(jù)示例相比,語句更為簡單,只需子句即可。 查詢語句 (DQL) 的構(gòu)造功能開發(fā)完畢,我們再給查詢構(gòu)造器增加一些對 DML (Data Manipulation Language) 語句的支持,如簡單的 insert、update、delete 操作。 insert 我們先回顧下 ...
說明:本篇主要學(xué)習(xí)數(shù)據(jù)庫連接階段和編譯SQL語句部分相關(guān)源碼。實(shí)際上,上篇已經(jīng)聊到Query Builder通過連接工廠類ConnectionFactory構(gòu)造出了MySqlConnection實(shí)例(假設(shè)驅(qū)動driver是mysql),在該MySqlConnection中主要有三件利器:IlluminateDatabaseMysqlConnector;IlluminateDatabaseQuery...
閱讀 857·2021-11-18 10:07
閱讀 2367·2021-10-14 09:42
閱讀 5376·2021-09-22 15:45
閱讀 602·2021-09-03 10:29
閱讀 3483·2021-08-31 14:28
閱讀 1887·2019-08-30 15:56
閱讀 3050·2019-08-30 15:54
閱讀 1005·2019-08-29 11:32