从数据库

时间:2016-03-27 00:47:54

标签: php html mysql database e-commerce

购买产品后,如何从产品中删除产品(购买的产品来自用户的购物车)?请询问您需要的更多代码以帮助我。 the cart table in my db

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



<!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>
&#13;
&#13;
&#13;

以上是我的cart.php页面(用户可以看到他们的物品,并且可以进入结账,这只是一个现在购买按钮,用于支付宝贝。一旦购买它就应该从我的数据库中删除它。

1 个答案:

答案 0 :(得分:0)

运行

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

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