【问题标题】:Removing table items from database从数据库中删除表项
【发布时间】:2016-03-27 00:47:53
【问题描述】:

购买后如何从我的表中删除产品(购买的产品来自用户的购物车)?请询问您需要的更多代码以尝试帮助我。

基本上所有的产品都是独一无二的,所以只有 1 个,所以一旦有人购买它就需要删除。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" href="assets/css/styles.css">

<?php 
session_start();
include 'conn.php';?>

</head>

<body>
<p> <b> <a href="cart.php" >SHOPPING CART</a> - </b> TOTAL ITEMS:<?php total_items ();?> TOTAL PRICE: <?php total_price ();?>  </p>
<?php echo $ip=getip();?>

<?php cart(); ?>
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="user_search" placeholder="search for products"/>
<input type="submit" name="search" value="search"/>
</form>



<section>
<div class="row">
<form action="" method="post" enctype="multipart/form-data">

<table class="align-center">

<tr>
<th> Remove </th>
<th> Product(s) </th>

<th> Total Price </th>
</tr>
<?php

$total = 0;
		global $conn;
		$ip = getip ();
		
		$select_price = " SELECT * FROM cart WHERE ip_add='$ip'";
		
		$run_price = mysqli_query ($conn, $select_price);
		
		while ($product_price = mysqli_fetch_array ($run_price)){
			
			$product_id = $product_price ['product_id'];
			$product_price ="SELECT * FROM products WHERE product_id='$product_id'";
			
			$run_product_price = mysqli_query($conn, $product_price);
			if (!$run_product_price) {
    printf("Error: %s\n", mysqli_error($conn));
    exit();
}
			
			while($pp_price = mysqli_fetch_array ($run_product_price)){
				$product_price = array ($pp_price ['product_price']);
				$product_title = $pp_price ['product_title'];
				$product_image = $pp_price ['product_image'];
				$single_price = $pp_price['product_price'];
				$values = array_sum($product_price);
				$total +=$values;
				

?>
<tr>
<td> <input type="checkbox" name="remove[]" value="<?php echo $product_id;?>"/> </td>
<td> <?php echo $product_title; ?> <br>
<img src="product_image/<?php echo $product_image; ?>" height="50" width="50"> </td>

<td>£<?php echo $values ?></td>

</tr>


<?php }
}
?>
<tr>
<td> <b> Sub Total: </b> </td>
<td> £<?php echo $total ?></td>
</tr>
<tr>
 <td><input type="submit" name="update_cart" value="Update Cart"> </td>
<td><input type="submit" name="continue" value="Continue Shopping "> </td>
<td><a href="checkout.php">CHECKOUT  </a> </td>

</tr>

</table>

</form>
<?php
		global $conn;

$ip = getip();
if(isset($_POST['update_cart'])){

foreach($_POST['remove'] as $remove_id){
	
	$del_product = "delete from cart where product_id='$remove_id' AND ip_add='$ip'";
	
	$run_del = mysqli_query($conn, $del_product);
	
	if($run_del){
		echo "<script> window.open ('cart.php', '_self') </script>";
	}
	
	
	
}
}

if(isset($_POST['continue'])){
echo "<script> window.open ('index.php', '_self') </script>";
}



?>

</section>

         
</div>
</body>
</html>

上面是我的 cart.php 页面(用户可以在其中看到他们的物品,并且可以去结账,这只是一个用于 paypal 付款的“立即购买”按钮。一旦它被购买,就应该从我的数据库中删除它。

【问题讨论】:

  • 您确定要从数据库中删除这些行吗?

标签: php html mysql database e-commerce


【解决方案1】:

运行

"DELETE FROM products WHERE product_id='$product_id'"

您应该知道,这种执行 SQL 查询的方法很容易受到 SQL 注入的攻击。我建议您改用参数化查询。

【讨论】:

  • 谢谢,我认为这应该可行,并感谢您告知我有关使用参数化查询的信息,需要查看一下。
猜你喜欢
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
相关资源
最近更新 更多