摘要:發(fā)現(xiàn)忘記的差不多了,也不會(huì)寫(xiě)了想著寫(xiě)點(diǎn)什么東西簡(jiǎn)單的寫(xiě)個(gè)購(gòu)物車(chē)類(lèi)請(qǐng)先登錄添加商品到購(gòu)物車(chē)修改購(gòu)買(mǎi)數(shù)量清空購(gòu)物車(chē)從購(gòu)物車(chē)中刪除商品處理數(shù)據(jù)簡(jiǎn)單使用添加到購(gòu)物車(chē)刪除指定的商品清空
發(fā)現(xiàn)pdo忘記的差不多了,sql也不會(huì)寫(xiě)了 想著寫(xiě)點(diǎn)什么東西 簡(jiǎn)單的寫(xiě)個(gè)購(gòu)物車(chē)類(lèi)
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(); } } //添加商品到購(gòu)物車(chē) 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"); } //修改購(gòu)買(mǎi)數(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"); } //清空購(gòu)物車(chē) 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"); } //從購(gòu)物車(chē)中刪除商品 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)); } //簡(jiǎn)單使用 $user = [ "host" => "", "user" => "root", "pwd" => "root", "db" => "shop", ]; $productid = intval($_POST["productid"]); $num = intval($_POST["num"]); $cart = new Cart($user); //添加到購(gòu)物車(chē) $cart->add_cart($productid, $num); //刪除指定的商品 $cart->remove_cart($productid); //清空 $cart->clear_cart(); ?>
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/23148.html
摘要:操作數(shù)據(jù)庫(kù)的種形式使用擴(kuò)展類(lèi)庫(kù)推薦使用擴(kuò)展類(lèi)庫(kù)這是類(lèi)庫(kù)的升級(jí)版,但已經(jīng)不推薦使用擴(kuò)展包含哪三個(gè)類(lèi)與區(qū)別可以支持多種數(shù)據(jù)庫(kù),而且操作方法一致只支持?jǐn)?shù)據(jù)庫(kù)如何使用連接數(shù)據(jù)庫(kù)什么是如何關(guān)閉連接通過(guò)來(lái)連接數(shù)據(jù)庫(kù),其中必須傳入數(shù)據(jù)源名稱(chēng)數(shù)據(jù)源名稱(chēng)是 PHP操作數(shù)據(jù)庫(kù)的2種形式 使用 PDO 擴(kuò)展類(lèi)庫(kù)(推薦) 使用 Mysqli 擴(kuò)展類(lèi)庫(kù)(這是Mysql類(lèi)庫(kù)的升級(jí)版,但已經(jīng)不推薦使用) PDO...
摘要:注在常駐內(nèi)存單例模式下,這種多次用一個(gè)類(lèi)進(jìn)行查詢(xún)的情形很常見(jiàn)。斷線重連對(duì)于典型環(huán)境而言,一次的查詢(xún)已經(jīng)隨著的請(qǐng)求而結(jié)束,的垃圾回收功能會(huì)回收一次請(qǐng)求周期內(nèi)的數(shù)據(jù)。但在常駐內(nèi)存的環(huán)境下,尤其是單例模式下,數(shù)據(jù)庫(kù)驅(qū)動(dòng)類(lèi)可能一直在內(nèi)存中不被銷(xiāo)毀。 構(gòu)造、執(zhí)行第一條語(yǔ)句 上一篇完成了代碼結(jié)構(gòu)的搭建和 PDO 的基礎(chǔ)封裝,這一篇我們來(lái)講如何構(gòu)造一個(gè)最基本的 SQL 語(yǔ)句,并執(zhí)行得到結(jié)果。 que...
摘要:同樣的,添加屬性,修改函數(shù)構(gòu)造語(yǔ)句的方法基類(lèi)中添加方法檢測(cè)有沒(méi)有設(shè)置子句構(gòu)建語(yǔ)句參數(shù)綁定返回影響的行數(shù)更新數(shù)據(jù)示例相比,語(yǔ)句更為簡(jiǎn)單,只需子句即可。 查詢(xún)語(yǔ)句 (DQL) 的構(gòu)造功能開(kāi)發(fā)完畢,我們?cè)俳o查詢(xún)構(gòu)造器增加一些對(duì) DML (Data Manipulation Language) 語(yǔ)句的支持,如簡(jiǎn)單的 insert、update、delete 操作。 insert 我們先回顧下 ...
說(shuō)明:本篇主要學(xué)習(xí)數(shù)據(jù)庫(kù)連接階段和編譯SQL語(yǔ)句部分相關(guān)源碼。實(shí)際上,上篇已經(jīng)聊到Query Builder通過(guò)連接工廠類(lèi)ConnectionFactory構(gòu)造出了MySqlConnection實(shí)例(假設(shè)驅(qū)動(dòng)driver是mysql),在該MySqlConnection中主要有三件利器:IlluminateDatabaseMysqlConnector;IlluminateDatabaseQuery...
閱讀 1181·2021-11-18 10:07
閱讀 2616·2021-10-14 09:42
閱讀 5701·2021-09-22 15:45
閱讀 748·2021-09-03 10:29
閱讀 3658·2021-08-31 14:28
閱讀 2061·2019-08-30 15:56
閱讀 3192·2019-08-30 15:54
閱讀 1163·2019-08-29 11:32