php购物车实现代码
PHP购物车实现代码
购物车功能是电子商务网站的核心部分,以下是一个简单的PHP购物车实现代码示例,包含添加商品、删除商品、更新数量和计算总价等功能。
数据库表结构
创建一个简单的products表和cart表:

CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10, 2),
description TEXT
);
CREATE TABLE cart (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
quantity INT,
session_id VARCHAR(100)
);
添加商品到购物车
<?php
session_start();
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
function addToCart($product_id, $quantity = 1) {
if (isset($_SESSION['cart'][$product_id])) {
$_SESSION['cart'][$product_id] += $quantity;
} else {
$_SESSION['cart'][$product_id] = $quantity;
}
}
?>
从购物车移除商品
function removeFromCart($product_id) {
if (isset($_SESSION['cart'][$product_id])) {
unset($_SESSION['cart'][$product_id]);
}
}
更新购物车商品数量
function updateCartItem($product_id, $quantity) {
if (isset($_SESSION['cart'][$product_id])) {
$_SESSION['cart'][$product_id] = $quantity;
}
}
计算购物车总价
function calculateTotal() {
$total = 0;
$db = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
foreach ($_SESSION['cart'] as $product_id => $quantity) {
$stmt = $db->prepare("SELECT price FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$product = $stmt->fetch();
$total += $product['price'] * $quantity;
}
return $total;
}
显示购物车内容
function displayCart() {
$db = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
echo "<table border='1'>";
echo "<tr><th>Product</th><th>Price</th><th>Quantity</th><th>Total</th><th>Action</th></tr>";
foreach ($_SESSION['cart'] as $product_id => $quantity) {
$stmt = $db->prepare("SELECT name, price FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$product = $stmt->fetch();
$item_total = $product['price'] * $quantity;
echo "<tr>";
echo "<td>{$product['name']}</td>";
echo "<td>\${$product['price']}</td>";
echo "<td><input type='number' value='{$quantity}' min='1' onchange='updateQuantity({$product_id}, this.value)'></td>";
echo "<td>\${$item_total}</td>";
echo "<td><button onclick='removeItem({$product_id})'>Remove</button></td>";
echo "</tr>";
}
echo "</table>";
echo "<p>Total: \$" . calculateTotal() . "</p>";
}
JavaScript辅助函数
function updateQuantity(product_id, quantity) {
window.location.href = 'update_cart.php?product_id=' + product_id + '&quantity=' + quantity;
}
function removeItem(product_id) {
window.location.href = 'remove_from_cart.php?product_id=' + product_id;
}
处理表单的PHP文件
创建add_to_cart.php:

<?php
session_start();
include 'cart_functions.php';
if (isset($_GET['product_id'])) {
addToCart($_GET['product_id'], $_GET['quantity'] ?? 1);
}
header('Location: products.php');
创建remove_from_cart.php:
<?php
session_start();
include 'cart_functions.php';
if (isset($_GET['product_id'])) {
removeFromCart($_GET['product_id']);
}
header('Location: cart.php');
创建update_cart.php:
<?php
session_start();
include 'cart_functions.php';
if (isset($_GET['product_id']) && isset($_GET['quantity'])) {
updateCartItem($_GET['product_id'], $_GET['quantity']);
}
header('Location: cart.php');
这个实现使用了PHP的Session来临时存储购物车数据,并与数据库交互获取商品详细信息。可以根据实际需求扩展功能,如添加用户认证、持久化存储购物车数据等。






