我正在尝试使用Ajax和多维会话数组创建一个购物车系统。该数组存储该id的产品ID和数量。我试图显示其中一个存储的id的数量,但不返回正确的值。
这是我的PHP代码
public function create()
{
session_start();
$product_id = $_GET['data'];
// Check if the cart has been created
if(isset($_SESSION['cart']) AND $_SESSION['cart'] != NULL)
{
// One or more items are have already been placed in the cart
}else{
// The cart is empty
$_SESSION['cart'] = array();
$_SESSION['cart'][$product_id] = array('quantity' => 3);
}
// Return the quantity of a stored product id
return $_SESSION['cart'][$product_id]['quantity'];
}
如您所见,应返回数字3。相反,我得到值1。