下面是我的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);}
请帮助谢谢。
答案 0 :(得分:2)
尝试使用writelog检查你的mysql语句是否正在获取值。这是一个小功能,可以帮助您更好地进行调试。
<?php
function writelog($content) {
$file = 'log.txt';
file_put_contents($file, date("Y-m-d H:i:s")." : ".$content."\r\n", FILE_APPEND | LOCK_EX);
}
//Only display php errors to the developer...
if($_SERVER['REMOTE_ADDR'] == "put your localhost ip address")
{
error_reporting(0);
ini_set('display_errors', 'On');
$db_hostname = "localhost";
$db_database = "ecommerce";
$db_username = "root";
$db_password = "";
}
else
{
error_reporting(0);
ini_set('display_errors', 'On');
$db_hostname = "localhost";
$db_database = "ecommerce";
$db_username = "root";
$db_password = "";
}
//Establishing database connection
$db_connection = mysqli_connect($db_hostname, $db_username, $db_password, $db_database) or die(mysqli_error($db_connection));
如何使用此功能的示例: 的 write_log($ delete_product)强>
这将创建一个文本文件,您可以在其中获得当前的php文件。 从这个网站学习一个完整的CRUD php mysqli示例:
答案 1 :(得分:1)
我认为你应该使用复选框
<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>
您应该在复选框中设置值= product_id,如
<input type="checkbox" name="remove[]" id="<?=$pp_price['product_id']?>>
答案 2 :(得分:1)
我得到了我的答案,我刚刚将以下值添加到输入复选框,它可以正常工作。
<input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?> ">
答案 3 :(得分:0)
试试这会对你有所帮助
$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>";
}
}
}