【问题标题】:insert into mysqli multiple values from foreach loop从 foreach 循环中插入 mysqli 多个值
【发布时间】:2016-03-20 20:42:04
【问题描述】:

如何通过foreach循环在sql中插入多个值?

(它只向我插入最后一个值而不是全部,如果我在 sql 中添加更多值,那么当在 foreach 循环中只有一个值时,它会复制我的插入数据。 如何在我的 sql 中添加唯一编号作为每次插入的订单 ID? 这是我的代码...

<?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'; ?>

如果您需要任何其他信息,请告知。

还有,为什么这个 else 语句不起作用。

 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');}
}

谢谢,

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    改用 PDO。让您免去 SQL 注入攻击的烦恼。

    <?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'; ?>
    

    【讨论】:

    • 请注意; mysqli 也准备了声明。
    【解决方案2】:

    您正在做的是向您的数据库发出一堆请求。我会这样做:

    //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 . ')';
    

    这样可以为数据库节省大量工作。我不确定它是否比使用你的 foreach 方法更便宜(如数据库性能),但它的编码更简洁。

    然后像你一样在你的 foreach 中计算一个新的数据列表:

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

    至于你的重定向问题:

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

    它不起作用,因为您可能在循环的早期打印了 html。打印 html 或使用重定向,但不能同时使用两者。您必须在重定向之前进行检查。 编辑:重定向不起作用,因为您在脚本的顶部打印了 html。

    【讨论】:

      【解决方案3】:

      $sql = "SELECT id,book, price from add_item WHERE id = ".$name; 如果您手动提出请求,这是否会给您几行代码?

      【讨论】:

      • 嗨,伙计们...感谢您为解决我的问题所做的努力,但我解决了...我使用 session 将所有订单添加到数据库中,然后将它们显示在 session Id=sessionid ( )... 更容易
      猜你喜欢
      • 2017-07-22
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多