[英]INSERT multiple records in MySQL with one PHP
無法弄清楚如何使用mysql將多個輸入插入數據庫。 我想要做的是制作一個訂單表格,您可以在其中插入多個商品,每個商品都有數量和價格。 我應該為每個輸入使用相同的字段名稱,后跟[]使其成為一個數組。 那么插入sql會是什么樣子?
<form action="processinvoice.php" method="post">
<label>Invoice Number</label>
<input type="text" name="invoicenumber" value="Enter Invoice Number">
<p><label>Vendor's ID</label>
<input type="text" name="vendorid" value="Enter Vendor's ID"></p>
<!-- Product code begining -->
<label>Items sold</label>
<select name="proid[]" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM products");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>
<?php
} ?>
</select>
<label>Quantity</label>
<input type="text" name="tquantity[]" value="Enter quantity">
<label>Price</label>
<input type="text" name="saleprice[]" value="Enter price">
<p></p><select name="proid[]" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM products");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>
<?php
} ?>
</select>
<label>Quantity</label>
<input type="text" name="tquantity[]" value="Enter quantity">
<label>Price</label>
<input type="text" name="saleprice[]" value="Enter price">
<p></p><select name="proid[]" >
<?php
$stmt = $dbcon->prepare("SELECT * FROM products");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>
<?php
} ?>
</select>
<label>Quantity</label>
<input type="text" name="tquantity[]" value="Enter quantity">
<label>Price</label>
<input type="text" name="saleprice[]" value="Enter price">
<!-- Product code END -->
<input type="submit" name="submit" value="submit">
我們要做的是:插入多個與數量和價格相關的物品。 所有項目將顯示相同的發票號和供應商ID。
如果要在MySQL DB中插入多個記錄:
INSERT INTO tbl_name(a,b,c)VALUES(1,2,3),(4,5,6),(7,8,9);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.