gpt4 book ai didi

php - 如何使用foreach循环在mysqli中插入多个 `product`和 `quantity`

转载 作者:行者123 更新时间:2023-11-29 01:39:55 27 4
gpt4 key购买 nike

我想使用 foreach 循环mysql 中插入每个产品的多个 productquantity 但代码如下仅插入我在输入字段中输入的最后一个产品。我使用 jquery 添加更多字段,这样我就可以输入更多 product 和每个产品的数量。

这是我用来在表单中添加更多输入字段的 jquery demo

<?php
if(isset($_POST['submit'])){
//process the form

$qtys = array($_POST["product_description"] => $quantity = $_POST["quantity"]);
foreach($qtys as $item => $qty){

$date = $_POST["date"];
$customer_name = $_POST["customer_name"];
$status = $_POST["status"];

$query = "INSERT INTO orders (";
$query .= "date, customer_name, product_description, quantity, status";
$query .= ") VALUES (";
$query .= "'{$date}', '{$customer_name}', '{$item}', {$qty}, {$status}";
$query .= ")";
$order_set = mysqli_query($connection, $query);
if($order_set){
redirect_to("index.php");
}

}



} else {
// failed

}


?>

我的表单

<form action="order.php" method="post">
<div class="newOrder">
<p><span>Date</span><input type="date" value="2014-12-01" name="date" /></p>
<p><span>Name</span>
<select name="customer_name">


<?php
while($customer = mysqli_fetch_assoc($customers_set)){ ?>

<option><?php echo $customer['customer_name']; ?></option>

<?php } ?>
<?php mysqli_free_result($customers_set); ?>

</select>

</p>


<div id="input_fields">

<p><span>Product Description</span>
<select name="product_description[]">
<?php
while($product = mysqli_fetch_assoc($product_set)){ ?>

<option><?php echo $product['product_description']; ?></option>

<?php } ?>
<?php mysqli_free_result($product_set); ?>
</select>
<input value="0" type="text" name="quantity []" />
</p>

</div>
<a href="#" class="more">Add More Product</a>


<p class="radio">
<input type="radio" name="status" value="0" checked />For delivery&nbsp;&nbsp;
<input type="radio" name="status" value="1" />For payment confirmation&nbsp;&nbsp;
<input type="radio" name="status" value="2" />Reserved items&nbsp;&nbsp;
</p>


<input type="submit" name="submit" value="Create Order" />
</div>
</form>

最佳答案

那是因为你在第一个循环的末尾进行重定向 - 将重定向移出循环 - 它应该循环并插入每个循环 - 这也是最明显的,但我忽略了你正在循环数组只有 1 个元素 - 因为您将它设置在 $qtys 的顶部 - 在 $_POST 上做一个 var dump 并让我看看发布了什么。

<?php

error_reporting(E_ALL);
if(isset($_POST['submit'])){


$errors = array();

$date = $_POST["date"];
$customer_name = $_POST["customer_name"];
$status = $_POST["status"];

foreach($_POST['product'] as $key => $desc){
// do this to other ones too
$qty = filter_var($_POST['quantity'][$key], FILTER_SANITIZE_NUMBER_INT);
$desc = filter_var($desc, FILTER_SANITIZE_STRING);

$query = "INSERT INTO orders (";
$query .= "date, customer_name, product_description, quantity, status";
$query .= ") VALUES (";
$query .= "'{$date}', '{$customer_name}', '{$desc}', {$qty}, {$status}";
$query .= ")";
$order_set = mysqli_query($connection, $query);
if(!$order_set){
$errors = $query;
}
}// end of for each

if(empty($errors)){
// no errors redirect to home page

header("Location: index.php");
}

// if it makes it here then there are errors.
echo ' Errors in the sql ---- ' ;
var_dump($errors);
echo 'POST VALUES';
var_dump($_POST);
}

?>












<form action="order.php" method="post">
<div class="newOrder">
<p><span>Date</span><input type="date" value="2014-12-01" name="date" /></p>
<p><span>Name</span>
<select name="customer_name">


<?php
while($customer = mysqli_fetch_assoc($customers_set)){ ?>

<option value="<?php echo $customer['customer_name']; ?>"><?php echo $customer['customer_name']; ?></option>

<?php } ?>
<?php mysqli_free_result($customers_set); ?>

</select>

</p>


<div id="input_fields">

<p><span>Product Description</span>
<select name="product[]">
<?php
while($product = mysqli_fetch_assoc($product_set)){ ?>

<option value="<?php echo $product['product_description']; ?>"><?php echo $product['product_description']; ?></option>

<?php } ?>
<?php mysqli_free_result($product_set); ?>
</select>
<input value="0" type="text" name="quantity[]" />
</p>

</div>
<a href="#" class="more">Add More Product</a>


<p class="radio">
<input type="radio" name="status" value="0" checked />For delivery&nbsp;&nbsp;
<input type="radio" name="status" value="1" />For payment confirmation&nbsp;&nbsp;
<input type="radio" name="status" value="2" />Reserved items&nbsp;&nbsp;
</p>


<input type="submit" name="submit" value="Create Order" />
</div>
</form>

关于php - 如何使用foreach循环在mysqli中插入多个 `product`和 `quantity`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286609/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com