如何添加多个数据在同一时间在我的sql与php相同的表


How to add multiple data in same time in my sql with php same table

我试图添加相同的时间输入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">&nbsp;</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>&nbsp;</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);
?>

您可以将提交的值存储在数组中,如下所示

$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对应双精度(浮点数),b对应二进制数据。

我还没有测试过,但是你可以试试这个想法