2016-03-27 14 views
-1

Wie kann ich ein Produkt nach dem Kauf aus meiner Tabelle entfernen (die gekauften Produkte stammen aus dem Einkaufswagen des Benutzers)? Bitte fragen Sie nach mehr Code, den Sie benötigen, um mir zu helfen. Entfernen von Tabellenelementen aus der Datenbank

im Grunde sind alle Produkte einzigartig, so gibt es nur 1 von ihnen alle, so dass es entfernt werden muss, sobald jemand es kauft.

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

 
?> 
 
<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 
 
\t \t global $conn; 
 

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

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

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

 

 

 
?> 
 

 
</section> 
 

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

Die oben ist meine cart.php Seite (wo die Nutzer ihre Einzelteile zu sehen und zur Kasse gehen kann, die jetzt nur noch eine Kaufen-Button für PayPal-Zahlungen ist. Sobald das Gerät gekauft wurde wann wird es sollte aus meiner db entfernt werden.

+0

Sind Sie sicher, dass die Zeilen aus der Datenbank gelöscht werden sollen? – Drew

Antwort

0

Run

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

Sie sollten sich bewusst sein aber, dass diese Methode der SQL-Abfragen sind anfällig für SQL-Injection. Ich würde empfehlen, dass Sie zu parametrisierten Abfragen wechseln.

+0

Danke, ich denke, das sollte funktionieren und danke, dass Sie mich über die Verwendung von parametrisierten Abfragen informiert haben, müssen Sie einen Blick darauf werfen. – SCJ