php+pdo实现的购物车类完整示例
(编辑:jimmy 日期: 2025/11/14 浏览:3 次 )
本文实例讲述了php+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=";
$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+";
$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 "update shop_cart set num=";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($num, $_SESSION['user_id'], $productid));
$rows = $stmt->rowCount();
return $rows "delete from shop_cart where userid=";
$stmt = $this->pdo->prepare($sql);
$this->pdo->execute(array($this->user_id));
$rows = $stmt->rowCount();
return $rows "delete from shop_cart where productid=";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($productid, $_SESSION['user_id']));
$rows = $stmt->rowCount();
return $rows "_blank" href="//www.jb51.net/Special/554.htm">PHP+MySQL购物车开发专题》、《php面向对象程序设计入门教程》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
下一篇:正版phpstorm免费激活步骤教程详解