[英]How can I add value in a session array every time I click add to cart button?
Everytime I click add cart button I pass an id value and get data from a database and I want to save it a session array. 每次我单击添加购物车按钮时,我都会传递一个id值并从数据库中获取数据,并且想要将其保存为会话数组。
<?php session_start() ?>
<?php require 'config.php' ?>
<?php
if (isset($_POST['id'])) {
$sql = "SELECT product_name, product_price FROM products WHERE id=".$_POST['id'];
$result = $mysqli->query($sql) or die($mysqli->error);
$result = $result->fetch_assoc();
if (isset($_SESSION['cart'])) {
if (isset($_SESSION['cart']['id'])) {
unset($_SESSION['cart']['id']);
}
}
$_SESSION['cart']['id'] = $result;
$cart = count($_SESSION['cart']);
//$cart = json_encode($_SESSION['cart']['id']);
echo $cart;
}
?>
Try something like this : 尝试这样的事情:
if(!isset($_SESSION['cart']))
$_SESSION['cart'] = array();//Declaration of the cart
$_SESSION['cart'][] = $result;//To add the result in cart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.