插入多行中的數據庫中,我創建了可以添加product_name
和product
與jquery
的幫助下quantity
更input field
一種形式,這是demo,您可以在表單中添加更多的輸入字段。使用PHP形式
問題是當我提交表格時,只有最後一個product
將提交到我的數據庫中,其餘的產品不會提交。
這是我的查詢
<?php
if(isset($_POST['submit'])){
//process the form
$date = $_POST["date"];
$customer_name = $_POST["customer_name"];
$product_description = $_POST["product_description"];
$quantity = $_POST["quantity"];
$status = $_POST["status"];
$query = "
INSERT INTO orders (
date, customer_name, product_description, quantity, status
) VALUES (
'$date', '$customer_name', '$product_description',$quantity,$status
)";
$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>
任何機構有任何想法如何提交所有product
和quantity
輸入input field
將保存在數據庫中。
我想你只需要構建值列表在一個循環,然後建立並執行整個查詢 – Strawberry 2014-12-04 01:10:11
感謝對你的建議,但我不知道從哪裏開始,因爲我的PHP學習是如此有限..但無論如何,謝謝 – jhunlio 2014-12-04 01:23:45