正在从数据库中删除 table 项

Removing table items from database

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

基本上所有产品都是独一无二的,所以只有 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 页面(用户可以在该页面看到他们的物品并可以转到结帐,这只是一个用于贝宝付款的立即购买按钮。一旦购买就应该将其删除来自我的数据库。

运行

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

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