【发布时间】:2016-07-15 05:38:26
【问题描述】:
下面是我的 php 代码,展示了我如何从购物车中删除商品。网页中没有显示错误,但该项目没有删除,购物车没有更新。 使用完整代码编辑
<div class="product_box">
<form action="cart.php" method="post" enctype="multipart/data">
<table align="center" width="700" bgcolor="skyblue">
<tr>
<th>Remove</th>
<th>Product (s) </th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
<?php
$total = 0;
global $db;
$ip = getIp();
$sel_price ="select * from cart where ip_add='$ip'";
$run_price = mysqli_query($db, $sel_price);
while ($p_price=mysqli_fetch_array($run_price)) {
$pro_id = $p_price['p_id'];
$pro_price = "select * from products where product_id='$pro_id'";
$run_pro_price = mysqli_query($db, $pro_price);
while ($pp_price = mysqli_fetch_array($run_pro_price)) {
$product_price = array($pp_price ['product_price']);
$product_title = $pp_price['product_title'];
$product_image = $pp_price['product_img1'];
$single_price = $pp_price['product_price'];
$values = array_sum($product_price);
$total +=$values;
//echo "Rs ." . $total;
?>
<tr align="center">
<td><input type="checkbox" name="remove[]"></td>
<td><?php echo $product_title; ?><br>
<img src="admin_area/product_images/<?php echo $product_image;?>" width="50px" height="50px">
</td>
<td><input type="text" name="qty" size="3"></td>
<td><?php echo "Rs." . $single_price ?></td>
</tr>
<?php } } ?>
<tr align="right">
<td colspan="3">
<b> Sub Total: </b>
</td>
<td>
<?php echo "Rs." .$total; ?>
</td>
</tr>
<tr align="center">
<td colspan="1"><input type="submit" name="update_cart" value="Update Cart"></td>
<td><input type="submit" name="continue" value="Continue Shopping"></td>
<td><button><a href="checkout.php">Checkout</a></button></td>
</tr>
</table>
</form>
<?php
$ip = getIp();
if (isset($_POST['update_cart'])) {
foreach ($_POST['remove'] as $remove_id) {
$delete_product = "delete from cart where p_id=".$remove_id." AND ip_add=".$ip;
$run_delete = mysqli_query($db, $delete_product);
if ($run_delete) {
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
}
?>
</div>
</div>
我尝试通过在 if 语句之后添加以下代码来检查错误,但没有成功。
else { echo mysqli_error($db);}
请帮忙谢谢。
【问题讨论】:
-
您是否检查过
AND ip_add='$ip'是否匹配?我的意思是即将到来的请求和存储在 Db 中的数据? -
是的。这是正确的。数据正在数据库中
-
@SagarKodte 请做一些调试。添加回显语句看看它在哪里刹车
-
调试一些信息 检查 $ip, $_POST 中的值。也许 $_POST['remove'] 不是一个数组?
-
注意: IP 地址可以共享,因此每个 IP 地址只能有一个购物车。这是一个不好的方法
标签: php mysql shopping-cart cart