To display the cart contents:
function displayCart()
if (empty($_SESSION['cart']))
echo "Cart is empty.";
return;
echo "Your Cart:<br>";
foreach ($_SESSION['cart'] as $item)
echo $item['name'] . " x " . $item['num'] . " = $" . ($item['price'] * $item['num']) . "<br>";
// Example usage
displayCart();
CREATE TABLE cart_items (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL CHECK (quantity > 0),
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY (user_id, product_id)
);