我正在使用HTML表格来收集用户的数据。通过单击“添加更多”按钮可以增加表的行,也可以通过单击“删除”按钮来减少表的行。因此,表中的行数对于所有用户都不是固定的。有时它只有4行,有时可能是12行。我想知道如何通过单击SUBMIT按钮一次在我的mysqli数据库中插入所有数据(行)。你可以在下面的链接中查看我的表格:
我的PHP代码:
<?php
$email = $_SESSION["email"];
$cell = "Select `cellno` FROM `userreg` WHERE email = '$email'";
$result = mysqli_query($conn, $cell);
$cellno = "";
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
$cellno = $row["cellno"];
}
}
if(isset($_POST['submit']))
{
$fullname = $_SESSION["fullname"];
$medname = $_POST['medname'];
$medqty = $_POST['medqty'];
$medtype = $_POST['medtype'];
$num = $_POST['num'];
$date = date("Y.m.d");
$medorder = "INSERT INTO `medorder` (`fullname`, `phoneno`, `medname`, `quantity`, `medtype`, `date`,`orderno` ) VALUES ('$fullname', '$cellno', '$medname','$medqty', '$medtype', CURRENT_TIMESTAMP, '$num' )";
if($conn->query($medorder)=== TRUE)
{
echo"<br>";
echo " Your Question has been submitted successfully";
}
else
{
echo " Error!! Try again or later";
}
}
?>
答案 0 :(得分:0)
首先更改输入的名称:
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD><input type="hidden" value="1" name="num[]" id="orderdata">1</TD>
<TD><INPUT type="text" name="medname[]" class="form-control" id="orderdata" placeholder="Your Medicine name"/></TD>
<TD><INPUT type="text" name="medqty[]" class="form-control" id="orderdata" placeholder="Quantity" /></TD>
<TD>
<SELECT name="medtype[]" class="form-control" id="orderdata">
<OPTION value="in">Bangladesh</OPTION>
<OPTION value="de">Germany</OPTION>
<OPTION value="fr">France</OPTION>
<OPTION value="us">United States</OPTION>
<OPTION value="ch">Switzerland</OPTION>
</SELECT>
</TD>
</TR>
然后通过循环表中的行数来构建查询。
$medorder='';
foreach ($medname as $key => $value) {
$medorder .= "INSERT INTO `medorder` (`fullname`, `phoneno`, `medname`, `quantity`, `medtype`, `date`,`orderno` ) VALUES ('$fullname', '$cellno[$key]', '$medname[$key]','$medqty[$key]', '$medtype[$key]', CURRENT_TIMESTAMP, '$num' ),";
}