Php 从foreach循环向mysqli插入多个值
如何从foreach循环在sql中插入多个值 (它只插入最后一个值,而不是全部,如果我在sql中添加更多值,那么当在foreach循环中仅插入一个值时,它将复制我的插入数据。 如何在我的sql中添加唯一的编号作为每次插入的订单id? 这是我的密码Php 从foreach循环向mysqli插入多个值,php,mysqli,Php,Mysqli,如何从foreach循环在sql中插入多个值 (它只插入最后一个值,而不是全部,如果我在sql中添加更多值,那么当在foreach循环中仅插入一个值时,它将复制我的插入数据。 如何在我的sql中添加唯一的编号作为每次插入的订单id? 这是我的密码 <?php include'header_nav.php'; ?> <div class="mainContent_u"> <h1> Your shopping Chart<br/>contains
<?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');}
}
foreach($\u发布为$name=>$value){
如果(是数值($value)&&$value!=0&&$name!=“提交”){
$sql=“从添加项目中选择id、书籍、价格,其中id=”.$name;
$result=mysqli\u查询($con,$sql);
$row=mysqli\u fetch\u assoc($result);
$price=$row['price']*$value;
$price=number\u格式($price,2);
$totalPrice=$totalPrice+$price;
$totalPrice=number\u格式($totalPrice,2);
$qty=$value;
$sql=“在订单中插入(订单id、书籍名称、数量、价格pb、价格总计)值(“$row['id']”、“$row['book']”、“$qty.”、“$row['price']”、“$price.”、“$price.”);
回声“;
回音“$row['book']”..$value.”.€“$row['price']”.€“$price.”;
回声“;
}else{header('location:all_books.php');}
}
谢谢,您正在对数据库执行一系列请求。我将执行以下操作:
//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。请改用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'; ?>
您的购物图表
包含。。。
书名
数量
价格
总计
查看您的购物车,然后进行结帐。
$sql=“从添加项中选择id、书籍、价格,其中id=”.$name;
如果您手动提出请求,这会给您几行吗?请注意;mysqli
也准备了语句。大家好……感谢您为解决我的问题所做的努力,但我解决了问题……我使用session在数据库中添加所有订单,然后在session Id=sessionid()的位置显示它们……这要容易得多