[英]How to create add value to array everytime i click submit button? in php (with session)
[英]How can I add value in a session array every time I click add to cart button?
每次我單擊添加購物車按鈕時,我都會傳遞一個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;
}
?>
嘗試這樣的事情:
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.