在此代码中,我想根据id覆盖cookie值。一旦你将一个值添加到id = 55的cookie中,并且如果我从相同的id添加新值,则使用相同id的新值更新的cookie值不会添加。
这是我的代码
<form method='post'>
<input type="text" hidden="hidden" value="<?php echo $prdctid;?>" name="id">
<input type="number" name="name" value="1" min="1" max="10" />
<input type="submit" id="submit" name="submit" value="Add" />
</form>
if(isset($_POST['submit'])){
$qtyprid = $_POST['id'];
$qtyval = $_POST['name'];
$cart_qty=array();
$cart_qty[$qtyval]=$qtyprid;
$cookie = $_COOKIE['qty_items_cookie'];
$cookie = stripslashes($cookie);
$saved_cart_qty = json_decode($cookie, true);
if(!$saved_cart_qty){
$saved_cart_qty=array();
}
if(count($saved_cart_qty)>0){
foreach($saved_cart_qty as $key=>$value){
$cart_qty[$key]=$value;
}
}
$json = json_encode($cart_qty, true);
setcookie('qty_items_cookie', $json);
}