在显示产品清单的索引页面上,将同一产品添加到购物车。
index.php
<?php
session_start();
include('php/config.php');
$status="";
if (isset($_POST['id_product']) && $_POST['id_product']!=""){
$id_product = $_POST['id_product'];
$sql = $mysqli->prepare("SELECT id_product,product,price,imagen FROM products WHERE id_product=?");
$sql->bind_param("i",$id_product);
$sql->execute();
$sql->bind_result($id_product,$product,$price,$imagen);
$sql->fetch();
$cartArray = array(
$id_product=>array(
'id_product'=>$id_product,
'product'=>$product,
'price'=>$price,
'quantity'=>1,
'imagen'=>$imagen
)
);
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $cartArray;
$status = "<div class='box'>The product was added to the cart!</div>";
} else {
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($id_product,$array_keys)) {
$status = "<div class='box' style='color:red;'>The product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge($_SESSION["shopping_cart"],$cartArray);
$status = "<div class='box'>The product was added to the cart!</div>";
}
}
}
?>
<div class="container">
<div class="starter-template center-screen">
<h1>List of products</h1>
<?php
$result = $mysqli->prepare("SELECT id_product,product,price,imagen FROM products");
$result->execute();
$result->store_result();
$result->bind_result($id_product,$product,$precio,$imagen);
while ($result->fetch()) {
echo "<div class='product_wrapper'>
<form method='post' action=''>
<input type='hidden' name='id_product' value=".$id_product." />
<div class='image'><img src='".$imagen."' /></div>
<div class='name'>".$product."</div>
<div class='price'>$".$price."</div>
<button type='submit' class='buy'>Buy Now</button>
</form>
</div>";
}
?>
</div>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
</div>
我在购物车的页面上显示并添加了消除产品的动作
cart.php
<?php
session_start();
$status="";
if (isset($_POST['action']) && $_POST['action']=="remove"){
if(!empty($_SESSION["shopping_cart"])) {
foreach($_SESSION["shopping_cart"] as $key => $value) {
if($_POST["id_product"] == $key){
unset($_SESSION["shopping_cart"][$key]);
$status = "<div class='box' style='color:red;'>The product is removed from your cart!</div>";
}
if(empty($_SESSION["shopping_cart"]))
unset($_SESSION["shopping_cart"]);
}
}
}
?>
<table class="table">
<tbody>
<tr>
<td></td>
<td>ARTICLE</td>
<td>QUANTITY</td>
<td>PRICE</td>
<td>SUBTOTAL</td>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td><img src='<?php echo $product['imagen']; ?>' width="50" height="40" /></td>
<td><?php echo $product['product']; ?><br>
<form method='post' action=''>
<input type='hidden' name='id_producto' value="<?php echo $product["id_product"]; ?>" />
<input type='hidden' name='action' value="remove" />
<button type='submit' class='remove'>Remove Item</button>
</form>
</td>
<td>
<?php echo $product['quantity']; ?>
</td>
<td><?php echo "$".$product["price"]; ?></td>
<td><?php echo "$".$product["price"]*$product["quantity"]; ?></td>
</tr>
<?php
$total_price += ($product["price"]*$product["quantity"]);
}
?>
<tr>
<td colspan="5" align="right">
<strong>TOTAL: <?php echo "$".$total_price; ?></strong><br>
</td>
</tr>
</tbody>
</table>
我提出的问题是产品被重复,并且不能消除重复的产品
我做错了什么,请帮帮我