1
每次點擊添加購物車按鈕我傳遞一個id值並從數據庫中獲取數據,我想將它保存爲一個會話數組。每次點擊添加到購物車按鈕時,如何在會話數組中添加值?
<?php session_start() ?>
<?php require 'config.php' ?>
<?php
if (isset($_POST['id'])) {
$sql = "SELECT product_name, product_price FROM products WHERE id=".$_POST['id'];
$result = $mysqli->query($sql) or die($mysqli->error);
$result = $result->fetch_assoc();
if (isset($_SESSION['cart'])) {
if (isset($_SESSION['cart']['id'])) {
unset($_SESSION['cart']['id']);
}
}
$_SESSION['cart']['id'] = $result;
$cart = count($_SESSION['cart']);
//$cart = json_encode($_SESSION['cart']['id']);
echo $cart;
}
?>
檢查這個http://stackoverflow.com/a/21652718/1723893 –