2016-03-20 8 views
0

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,

Antwort

0

Was Sie tun, ist eine Reihe von Anfragen an Ihre Datenbank. Ich würde so etwas tun:

//first create something like: $id_seq= '(1,2,3,56,7674,234)'; 
$id_seq = implode(',',$POST); 
//okay I didn't clean up the Post, but you'll get the picture hopefully :) 

//Then pull your request in one go: 
$sql = 'SELECT id,book, price from add_item WHERE id IN (' . $id_seq . ')'; 

So speichern Sie Ihre Datenbank eine Menge Arbeit. Ich bin mir nicht sicher, ob es weniger teuer ist (wie in Datenbank-Performance) als mit Ihrer Foreach-Methode, aber es ist eine viel bessere Codierung.

foreach ($list as $rec){ 
    $price = $rec['price'] * $_POST[$rec['id']]; 
    //complete your price and insert it in the orders-table 
} 

Wie für Ihre Umleitung-Frage:

Dann in Ihrem foreach eine neue Liste von Daten wie Sie haben, berechnen

else {header('location:all_books.php');} 
} 

Es wird nicht funktionieren, weil Sie html gedruckt haben könnte früher in der Schleife. Entweder drucken Sie die HTML oder verwenden Sie eine Weiterleitung, aber nicht beide. Sie müssen einen Check durchführen, bevor Sie umleiten. EDIT: die Umleitung wird nicht funktionieren, seit Sie html im oberen Teil des Skripts gedruckt haben.

0

Verwenden Sie stattdessen PDO. Es erspart Ihnen die Kopfschmerzen von SQL-Injection-Attacken.

<?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);  
    while($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 . "')"; 
     mysqli_query($con, $sql); 
     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'; ?> 
+0

Beachten Sie, dass; mysqli hat auch Aussagen vorbereitet. – HddnTHA

0

$sql = "SELECT id,book, price from add_item WHERE id = ".$name; Heißt das, Sie mehrere Zeilen geben, wenn Sie die Anforderung manuell machen?

+0

Hallo Leute ... Danke für Ihre Bemühungen, um mein Problem zu lösen, aber ich löse es ... Ich benutze Sitzung, um alle Bestellungen in der Datenbank hinzuzufügen und dann zeigen Sie, wo Sitzungs-ID = sessionid() ... viel einfacher – Rogi

Verwandte Themen