what's wrong in below code?
if(isset($_POST["product_code"]) && isset($_POST["product_color"]))
{
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_color"] = $_POST["product_color"];
if(isset($_SESSION["products"])){ //if session var already exist
$check = $_SESSION["products"][$new_product['product_code']][$new_product['product_color']];
if(isset($check)) //check item exist in products array
{
unset($check); //unset old item
}
}
$check = $new_product; //update products with new item array
}
$total_items = count($_SESSION["products"]); //count total items
die(json_encode(array('items'=>$total_items))); //output json
}
when I have added product_color
then product is not getting added. What's wrong while writing the array syntax or checking the product_code
and product_color
present in cart or not?
You have to assign the product_color
to hidden variable in index page. Then only you can added in $new_product["product_color"]=$_POST["product_color"];
Example
<input type="hidden" name="product_color" value=<?php Echo $obj->product_color;?> />