不从购物车中删除项目
Not removing item from the cart
下面是我的 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);}
请帮忙谢谢。
试试这个对你有帮助
$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>";
}
}
}
尝试使用 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 示例:
我认为你应该在带有复选框的行中
<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']?>>
我得到了答案我只是在输入复选框中添加了以下值并且它有效。
<input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?> ">
下面是我的 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);}
请帮忙谢谢。
试试这个对你有帮助
$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>";
}
}
}
尝试使用 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 示例:
我认为你应该在带有复选框的行中
<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']?>>
我得到了答案我只是在输入复选框中添加了以下值并且它有效。
<input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?> ">