I have a PHP cart that is using sessions. I am using ajax for the add to cart and delete operations. As for now, I can perfectly add products to the session. The problem comes when I delete an item, the items will be successfully deleted, but, the cart misbehaves when I try to add new items after deleting some instead of adding items like before, it will be now updating the first element in the cart.
the code for add to cart is
<?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';
?>
the code for delete is
<?php
session_start();
$key = $_POST['delid'];
echo $key;
unset($_SESSION['cart'][$key]);
?>