Php 添加相同产品时增加$\会话中的数量值
我想通过单击“添加到购物车”来添加更多数量,但我的代码显示,它的最大数量为2 我的代码: cart\u process.phpPhp 添加相同产品时增加$\会话中的数量值,php,session,Php,Session,我想通过单击“添加到购物车”来添加更多数量,但我的代码显示,它的最大数量为2 我的代码: cart\u process.php if(isset($_POST["product_code"])) { foreach($_POST as $key => $value){ $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING); //create a new product array }
if(isset($_POST["product_code"]))
{
foreach($_POST as $key => $value){
$new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING); //create a new product array
}
//we need to get product name and price from database.
$statement = $mysqli_conn->prepare("SELECT product_name, product_price FROM products_list WHERE product_code=? LIMIT 1");
$statement->bind_param('s', $new_product['product_code']);
$statement->execute();
$statement->bind_result($product_name, $product_price);
while($statement->fetch()){
$new_product["product_name"] = $product_name; //fetch product name from database
$new_product["product_price"] = $product_price; //fetch product price from database
$new_product["product_qty"] = 1;
if(isset($_SESSION["products"])){ //if session var already exist
if(isset($_SESSION["products"][$new_product['product_code']])) //check if item already exist in products array add more quantity +1
{
$_SESSION["products"][$new_product['product_code']] = $new_product["product_qty"]++;
}
}
$_SESSION["products"][$new_product['product_code']] = $new_product; //update products with new item array //update products with new item array
}
$total_items = count($_SESSION["products"]); //count total items
die(json_encode(array('items'=>$total_items))); //output json
}
第一次单击“添加到购物车”时的输出:
Array
(
[products] => Array
(
[TSH1] => Array
(
[product_color] => Red
[product_size] => M
[product_code] => TSH1
[product_name] => Cool T-shirt
[product_price] => 8.50
[product_qty] => 1
)
)
)
并在第二次单击时输出将同一产品再次添加到购物车:
[product_qty] => 2 // Quantity has been added when I click add the same product to cart
但当我再次单击“将同一产品添加到购物车”时,它不会添加更多数量。取消设置[产品数量]并增加计数器。。。添加回会话数组取消设置[产品数量]并增加计数器。。。添加回会话数组
unset($\u会话[“产品”][$new\u产品['product\u数量]])
对吗?unset($\u会话[“产品”][$new\u产品['product\u数量]])
对吗?