我正在尝试添加同时输入20条记录。当我提交按钮时,所有数据都存储在mysql同一个表中。但是现在只添加了一行。你能帮我看一下如何在同一张桌子上添加20行或更多行。
<form action="codeexecdealership.php" method="post" name="reg" onsubmit="return confirm('Trasanction Amount: ' + document.getElementById('submit').value + '\n\nSelect OK if correct or Cancel to edit.')">
<table style="width: 350px;" border="0" cellspacing="0" cellpadding="2" align="center">
<tbody>
<tr>
<td colspan="2"> </td>
<tr>
<th>stockdate</th>
<th>invoicedate</th>
<th>companyname</th>
<th>companyinvoiceno</th>
<th>productcode</th>
<th>productname</th>
<th>buycost</th>
<th>salecost</th>
<th>quantitypice</th>
<th>quantitycartoon</th>
<th>warehouse</th>
<th>freeproductqtp</th>
<th>freeproductqtc</th>
</tr>
<tr>
<td>
<input type="text" name="stockdate" value="<?php echo date("M j, Y"); ?>"/>
</td>
<td>
<input type="text" name="invoicedate" value="<?php echo date("M j, Y"); ?>"/>
</td>
<td>
<select class="companyname" name="companyname">
<option value="GLOBSOFTDRINKSLTD" value="GLOBSOFTDRINKSLTD">GLOBSOFTDRINKSLTD</option>
<option value="ASFAMARKETING" value="ASFAMARKETING">ASFAMARKETING</option>
<option value="PRANCF" value="PRANCF">PRANCF</option>
<option value="PRANFNR" value="PRANFNR">PRANFNR</option>
<option value="PRANBC" value="PRANBC">PRANBC</option>
</select>
</td>
<td><input type="text" id="companyinvoiceno" name="companyinvoiceno" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="productcode" name="productcode" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="productname" name="productname" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="buycost" name="buycost" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="salecost" name="salecost" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="quantitypice" name="quantitypice" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="quantitycartoon" name="quantitycartoon" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td>
<select class="warehouse" name="warehouse">
<option value="FACTORY" value="FACTORY">FACTORY</option>
<option value="GODAUN" value="GODAUN">ASFAMARKETING</option></td>
<td><input type="text" id="freeproductqtp" name="freeproductqtp" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="freeproductqtc" name="freeproductqtc" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
</tr>
<tr>
<td>
<input type="text" name="stockdate" value="<?php echo date("M j, Y"); ?>"/>
</td>
<td>
<input type="text" name="invoicedate" value="<?php echo date("M j, Y"); ?>"/>
</td>
<td>
<select class="companyname" name="companyname">
<option value="GLOBSOFTDRINKSLTD" value="GLOBSOFTDRINKSLTD">GLOBSOFTDRINKSLTD</option>
<option value="ASFAMARKETING" value="ASFAMARKETING">ASFAMARKETING</option>
<option value="PRANCF" value="PRANCF">PRANCF</option>
<option value="PRANFNR" value="PRANFNR">PRANFNR</option>
<option value="PRANBC" value="PRANBC">PRANBC</option>
</select>
</td>
<td><input type="text" id="companyinvoiceno" name="companyinvoiceno" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="productcode" name="productcode" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="productname" name="productname" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="buycost" name="buycost" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="salecost" name="salecost" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="quantitypice" name="quantitypice" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="quantitycartoon" name="quantitycartoon" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td>
<select class="warehouse" name="warehouse">
<option value="FACTORY" value="FACTORY">FACTORY</option>
<option value="GODAUN" value="GODAUN">ASFAMARKETING</option></td>
<td><input type="text" id="freeproductqtp" name="freeproductqtp" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
<td><input type="text" id="freeproductqtc" name="freeproductqtc" maxlength="20" onkeypress="return isNumberKey(event)" required/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" id="submit" name="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
这是php代码
<?php
session_start();
include('connection.php');
$id=$_POST['id'];
$stockdate=$_POST['stockdate'];
$invoicedate=$_POST['invoicedate'];
$companyname=$_POST['companyname'];
$companyinvoiceno=$_POST['companyinvoiceno'];
$productcode=$_POST['productcode'];
$productname=$_POST['productname'];
$buycost=$_POST['buycost'];
$salecost=$_POST['salecost'];
$quantitypice=$_POST['quantitypice'];
$quantitycartoon=$_POST['quantitycartoon'];
$warehouse=$_POST['warehouse'];
$freeproductqtc=$_POST['freeproductqtc'];
$freeproductqtp=$_POST['freeproductqtp'];
mysql_query("INSERT INTO asfadealership(id, stockdate, invoicedate, companyname, companyinvoiceno, productcode, productname, buycost, salecost, quantitypice, quantitycartoon, warehouse, freeproductqtc, freeproductqtp)VALUES('$id', '$stockdate', '$invoicedate', '$companyname', '$companyinvoiceno', '$productcode', '$productname', '$buycost', '$salecost', '$quantitypice', '$quantitycartoon', '$warehouse', '$freeproductqtc', '$freeproductqtp')");
header("location: index.php?remarks=success");
mysql_close($con);
?>
答案 0 :(得分:0)
您可以将提交的值存储在数组中并执行此操作
$query = mysqli_prepare("INSERT INTO asfadealership(id, stockdate, invoicedate, companyname, companyinvoiceno, productcode, productname, buycost, salecost, quantitypice, quantitycartoon, warehouse, freeproductqtc, freeproductqtp)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
foreach($array_of_values as $key => $value) {
$query->bind_param('ssssssssssssss',$value['id'], $value['stockdate'], $value['invoicedate'], $value['companyname'], $value['companyinvoiceno'], $value['productcode'], $value['productname'], $value['buycost'], $value['salecost'], $value['quantitypice'], $value['quantitycartoon'], $value['warehouse'], $value['freeproductqtc'], $value['freeproductqtp']);
$query->execute();
}
bind_param()的第一个参数告诉它将绑定多少个值以及每个值的类型。 s对应于字符串数据,i对应于整数数据,d对应于double(浮点),b对应于二进制数据。
我没有测试过,但你可以试试这个想法