Product does not get added into cart. I get session id and checked session created. I can clearly see session data.
<?php
session_start();
$item_name = $_POST['itemname'];
$item_price = $_POST['itemprice'];
$item_weight = $_POST['itemweight'];
$item_quantity = $_POST['qty'];
// Add Item
if(isset($_POST['addcart'])){
$_SESSION['cart'][] = array('itemname' => $item_name, 'itemprice' => $item_price, 'itemweight' => $item_weight, 'itemquantity' => $item_quantity);
header("location:cart.php");
}
// Remove Item
if(isset($_POST['remove'])){
foreach($_SESSION['cart'] as $key => $value){
if($value['itemname'] === $_POST['item']){
unset($_SESSION['cart'][$key]);
$_SESSION['cart'] = array_values($_SESSION['cart']);
header('location:cart.php');
}
}
}
// Update Item
if(isset($_POST['edit'])){
foreach($_SESSION['cart'] as $key => $value){
if($value['itemname'] === $_POST['item']){
$_SESSION['cart'][$key] = array('itemname' => $item_name, 'itemprice' => $item_price, 'itemweight' => $item_weight, 'itemquantity' => $item_quantity);
header('location:cart.php');
}
}
}
?>
<form action="insertcart.php" method="post">
<a id="link" href="category/book.php?id=<?php echo $list['id']?>">
<h6 style="color:#000000;"><?php echo $list['book_name']?></h6>
<h6 style="color:#000000;">By <span><?php echo $list['author_name']?></span></h6>
<h6><del style="color:#dd3333;">₹<Span><?php echo $list['mrp']?></Span></del><span style="margin-left:10px; color:#228B22; font-weight:bold;">₹<Span><?php echo $list['selling_price']?></Span></span></h6>
</a>
<input type="hidden" name="itemname" value="<?php echo $list['book_name']?>">
<input type="hidden" name="itemprice" value="<?php echo $list['selling_price']?>">
<input type="hidden" name="itemweight" value="<?php echo $list['weight']?>">
<span style="font-weight:bold;">QTY:
<select name="qty" id="qty">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
</span>
<div style="margin-top:20px;">
<input type="submit" name="addcart" class="btn btn-primary btn-block" value="Add to Cart">
</div>
</div>
</form>
<?php
session_start();
$count = 0;
if(isset($_SESSION['cart'])){
$count = count($_SESSION['cart']);
}
?>
Why product is not added into cart I tried everything i could but cart is empty due session breaks. What can I do? Its a session cart. Cookie also created. Could it be a PHP version issue?