我想使用product
在quantity
中插入每个产品的多个mysql
和foreach loop
,但下面的代码只插入我在input field
中输入的最后一个产品。我使用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
<input type="radio" name="status" value="1" />For payment confirmation
<input type="radio" name="status" value="2" />Reserved items
</p>
<input type="submit" name="submit" value="Create Order" />
</div>
</form>
答案 0 :(得分:3)
那是因为你在第一个循环结束时重定向 - 将重定向移出循环 - 它应该循环并插入每一个 - 也是最明显但我忽略了你循环通过一个数组只有1个元素 - 因为你在顶部$ qtys设置它 - 在$ _POST上做一个var转储,让我看看发布了什么。
<?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
<input type="radio" name="status" value="1" />For payment confirmation
<input type="radio" name="status" value="2" />Reserved items
</p>
<input type="submit" name="submit" value="Create Order" />
</div>
</form>
答案 1 :(得分:1)
我认为你需要使用输入名称作为数组
错误是您的数据过度使用print_r($_POST)
以检查是否已发布所有值
使用
<input type="text" name="product []" />
<input type="text" name="quantity []" />
<input type="text" name="product []" />
<input type="text" name="quantity []" />
<input type="text" name="product []" />
<input type="text" name="quantity []" />
你的if会让你到index
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");
// }
can use "if(!$order_set)" instead
}
答案 2 :(得分:0)
尝试 -
$query = "INSERT INTO orders (";
$query .= "date, customer_name, product_description, quantity, status";
$query .= ") VALUES ";
$values = array();
$date = $_POST["date"];
$customer_name = $_POST["customer_name"];
$status = $_POST["status"];
foreach($qtys as $item => $qty){
$values[] = "('{$date}', '{$customer_name}', '{$item}', {$qty}, {$status})";
}
$query .= implode(',', $values);
答案 3 :(得分:0)
根据此代码,我认为必须发布持久连接
请在每次插入后关闭连接尝试此操作。并且还考虑 redirect_to()也是错误的。