目标:
$prod_id
$_SESSION['cart']
$_SESSION
问题:
$_SESSION
未正确存储来自MySQL数据库产品的信息我的添加到购物车功能的代码
<?php
// If User Pushes Add Cart Button --------//
function cart(){
global $dbc;
if(isset($_GET['add_cart'])){
$prod_id = $_GET['add_cart']; // gets the product id from add_cart link extension
// Check to see if product is already in the session
if(isset($_SESSION['cart'][$prod_id])) { // if prod id is already in the session
$_SESSION['cart'][$prod_id]['quantity']++; //increase the qty of the product
} else { //if product is NOT in the session
//check the PRODUCTS DB for the product id
$sql = mysqli_query($dbc,"SELECT * FROM products WHERE id_product = '$prod_id'") or die (mysqli_error($dbc));
if(mysqli_num_rows($sql) != 0 ){ //if the results of this search is NOT empty
while ($row = mysqli_fetch_array($sql)){;//get each row
//store array $row into the SESSION array
$_SESSION["cart"][$row['id_product']]= array(
"quantity" => 1,
"price" => $row['price']);
}
}else{
$message = "The product could not be added.";
}}}}
?>