I have php mysql shopping cart which works well, but problem is that if I add product which has code numbers only without any letter, product is added multiple time. For example product code: K001 is working ok, but if product code is 001 without K I have same problem.
$cartArray = array(
$code=>array(
'name'=>$name,
'code'=>$code,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
$status = "<div class='box'>Product is added to your cart!</div>";
}else{
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($array_keys, $code)) {
$status = "<div class='box' style='color:red;'>
Product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge(
$_SESSION["shopping_cart"],
$cartArray
);
$status = "<div class='box'>Product is added to your cart!</div>";
}
}
}
?>
I added please take a look, but have same problem
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$code = strval($row['code']);
$price = $row['price'];
$image = $row['file_name'];
$cartArray = array(
$code=>array(
'name'=>$name,
'code'=>$code,
'price'=>$price,
'quantity'=>1,
'image'=>$image)
);