我有一个使用会话的PHP购物车。我将ajax用于添加到购物车和删除操作。就目前而言,我可以完美地将产品添加到会话中。问题出在我删除项目时,这些项目将被成功删除,但是,当我尝试在删除一些项目后添加新项目而不是像以前那样添加项目时,购物车行为异常,现在它将更新购物车中的第一个元素。
添加到购物车的代码为
<?php
include('session.php');
include('../config.php');
$qty = $_POST['qty'];
$id = $_POST['cartid'];
$name = $_POST['name'];
$price = $_POST['price'];
$image = $_POST['image'];
$description = $_POST['description'];
$cart_item = array(
"product_id" => $id,
"name" => $name,
"price" => $price,
"quantity" => $qty,
"image" => $image,
"description" => $description);
if(isset($_SESSION['cart'])){
$count = count($_SESSION['cart']);
$_SESSION['cart'][$count] = $cart_item;
}else{
$_SESSION['cart'][0] = $cart_item;
}
include 'cart.php';
?>
要删除的代码是
<?php
session_start();
$key = $_POST['delid'];
echo $key;
unset($_SESSION['cart'][$key]);
?>