Wie man mehrfachen Wert in sql von foreach Schleife einfügt?fügen Sie in mysqli mehrere Werte von foreach Schleife ein
(Es fügt nur den letzten Wert nicht alle, und wenn ich mehr Werte in SQL hinzufügen, dann wenn in der foreach-Schleife nur einen Wert es meine Daten einfügen. Wie eindeutige Nummer in meinem SQL als eine Auftrags-ID mit jedem Einsatz? Hier ist mein Code ...
<?php include'header_nav.php'; ?>
<div class="mainContent_u">
<h1> Your shopping Chart<br/>contains ...</h1>
<table class="show_cart">
<tr>
<th><strong><h3>Book Title</strong></h3></th>
<th><strong><h3>Quantity</strong></h3></th>
<th><strong><h3>Price</strong></h3></th>
<th><strong><h3>Total</strong></h3></th>
</tr>
<?php
require 'connect_db.php';
$totalPrice = 0;
foreach($_POST as $name => $value){
if(is_numeric($value) && $value != 0 && $name != "submit"){
$sql = "SELECT id,book, price from add_item WHERE id = ".$name;
$result=mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
$price = $row['price'] * $value;
$price = number_format($price,2);
$totalPrice = $totalPrice + $price;
$totalPrice = number_format($totalPrice,2);
$qty = $value;
$sql = "INSERT INTO orders (order_id,book_name, quantity, price_pb, price_total) VALUES ('" . $row['id'] . "','" . $row['book'] . "','" . $qty . "','" . $row['price'] . "','" . $price . "')";
echo "<tr>";
echo "<td>".$row['book']."</td><td>".$value."</td><td>€".$row['price']."</td><td>€".$price."</td>";
echo "</tr>";
}
}
echo "</table>";
echo "<strong><h3>Total Price: €".$totalPrice."</h3></strong>";
?>
<p><strong>Review your shopping cart and then proceed to checkout.</strong></p>
<a href="checkout.php"><button class="submit" >Proceed to Checkout</button></a>
</div>
<?php include'footer.php'; ?>
Wenn Sie andere Informationen benötigen wissen lassen.
Und als gut, warum diese else-Anweisung nicht funktioniert.
foreach($_POST as $name => $value){
if(is_numeric($value) && $value != 0 && $name != "submit"){
$sql = "SELECT id,book, price from add_item WHERE id = ".$name;
$result=mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
$price = $row['price'] * $value;
$price = number_format($price,2);
$totalPrice = $totalPrice + $price;
$totalPrice = number_format($totalPrice,2);
$qty = $value;
$sql = "INSERT INTO orders (order_id,book_name, quantity, price_pb, price_total) VALUES ('" . $row['id'] . "','" . $row['book'] . "','" . $qty . "','" . $row['price'] . "','" . $price . "')";
echo "<tr>";
echo "<td>".$row['book']."</td><td>".$value."</td><td>€".$row['price']."</td><td>€".$price."</td>";
echo "</tr>";
}else {header('location:all_books.php');}
}
Danke,
Beachten Sie, dass; mysqli hat auch Aussagen vorbereitet. – HddnTHA