[英]How to insert multiple rows in a table at a time
我有一個MySQL數據庫和PHP形式。
當用戶提交詳細信息時,我已成功更新到MySQL數據庫。
現在我有一個問題。
我在同一網頁中有5個具有相同字段的表格。 現在,用戶提交所有5個表單,然后單擊“提交”按鈕,然后將詳細信息保存到MySQL數據庫中。
我知道代碼,如果用戶提交單個表單如何保存到數據庫中。 但是我需要提交多個具有相同字段的表單,以及如何保存在數據庫中。
如果用戶提交單個表單,我的代碼將如下所示。
<?
if( $_POST )
{
$con = mysql_connect("xx","xx","xx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xx", $con);
$users_id = $_POST['id'];
$users_name = $_POST['name'];
$users_Telephone = $_POST['Telephone'];
$users_E_mail = $_POST['E_mail'];
$users_country = $_POST['country'];
$users_visa_categeory = $_POST['visa_categeory'];
$users_other_category = $_POST['other_category'];
$users_passport_no = $_POST['passport_no'];
$users_remarks = $_POST['remarks'];
$users_date = $_POST['date'];
$query = "
INSERT INTO `xx`.`xx` (
`id`,
`name`,
`Telephone`,
`E_mail`,
`country`,
`visa_categeory`, `other_category`, `passport_no`, `remarks`, `date`
)
VALUES ('$users_id', '$users_name', '$users_Telephone', '$users_E_mail',
'$users_country', '$users_visa_categeory', '$users_other_category', '$users_passport_no', '$users_remarks', '$users_date'
);";
mysql_query($query);
echo"<br /><br />";
echo" &n bsp; ";
printf(" <p> &nbs p;
& nbsp;
Your Reference id is %d\n (Please note this reference id for future)<p>", mysql_insert_id());
mysql_close($con);
}
?>
<table style="
background-color:#999;
color:#000;
text-align: left; width:500px; margin-left:250px; margin-top:20px;" border="1">
<tr>
<td style="color:#fff; text-indent:10px">Your Name:</td>
<td style="color:#00F; text-indent:10px"><?php echo $_POST["name"]; ?></td>
</tr>
<tr> <td style="color:#fff; text-indent:10px">Telephone:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["Telephone"]; ?> </td>
<tr> <td style="color:#fff; text-indent:10px">E_mail:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["E_mail"]; ?></td> </tr>
<tr> <td style="color:#fff; text-indent:10px">Country:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["country"]; ?> </td> </tr>
<tr> <td style="color:#fff; text-indent:10px">Visa_Categeory:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["visa_categeory"]; ?></td> </tr>
<tr> <td style="color:#fff; text-indent:10px">Other_Category:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["other_category"]; ?> </td> </tr>
<tr> <td style="color:#fff; text-indent:10px">Passport_No:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["passport_no"]; ?></td> </tr>
<tr> <td style="color:#fff; text-indent:10px" valign="top">Remarks:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["remarks"]; ?> </td> </tr>
<tr> <td style="color:#fff; text-indent:10px" valign="top">Date:</td><td style="color:#00F; text-indent:10px"><?php echo $_POST["date"]; ?> </td> </tr>
</tr>
</table>
您應該使用特殊名稱來命名每個“表單”輸入。 一個可能是在每個輸入后面附加一個數字,以便它可以檢測出您所使用的形式。
然后,只需使用逗號連接多個VALUES
:
<?
// You may know the form count?
define('NUM_FORMS', 5);
if( $_POST )
{
$con = mysql_connect("xx","xx","xx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xx", $con);
$userBlocks = array();
for ($i=0; $i < NUM_FORMS; $i++) {
$users_id = $_POST['id'.$i];
$users_name = $_POST['name'.$i];
$users_Telephone = $_POST['Telephone'.$i];
$users_E_mail = $_POST['E_mail'.$i];
$users_country = $_POST['country'.$i];
$users_visa_categeory = $_POST['visa_categeory'.$i];
$users_other_category = $_POST['other_category'.$i];
$users_passport_no = $_POST['passport_no'.$i];
$users_remarks = $_POST['remarks'.$i];
$users_date = $_POST['date'.$i];
// Add each 'user query' into an array
$userBlocks[] = "('$users_id', '$users_name', '$users_Telephone', '$users_E_mail',
'$users_country', '$users_visa_categeory', '$users_other_category', '$users_passport_no', '$users_remarks', '$users_date'
)";
}
if(!empty($userBlocks))
{
$query = "
INSERT INTO `xx`.`xx` (
`id`,
`name`,
`Telephone`,
`E_mail`,
`country`,
`visa_categeory`, `other_category`, `passport_no`, `remarks`, `date`
)
VALUES ".implode(", ", $userBlocks).";";
// Then implode the queries split by commas
mysql_query($query);
}
您不應同時提交多個表格。 我能想到的唯一方法是使用AJAX和Javascript,但這並不是一個好的解決方案。
您應該尋找另一種方式來完成您想要的。 看來您的方式有誤。
您可以通過多次調用insert
語句在表中插入多行。 您可以循環執行此操作,例如:
//getting the connection...
$conn = $this->_db->getConn();
for($a = 0; $a<5; $a++){
$query = $conn->prepare('Insert into reservation (name, idUser, text) values ('a', '132', 'cccc')');
$query->execute();
}
您一次只能從頁面提交一個表單。
您將需要為表單元素提出一個命名方案,以便在提交表單元素時可以取出各個表單元素。
例如。 像這樣給HTML
<form method=post>
<input name="formA_Name">
<input name="formA_Passport">
....
<input name="formB_Name">
<input name="formB_Passport">
....
<input name="formB_Name">
<input name="formB_Passport">
....
<input type=submit>
</form>
您提交到如下所示的php頁面,然后取出所需的內容。
//Pull details out into seperate arrays
foreach ($_POST as $Key = > $Value)
{
list($Form, $KeyName) = explode('_', $Key);
$forms[$Form][$KeyName] = $Value;
}
//This creates arrays like this
// ['FormA']['FormA_Name'] = 'Name Submitted'
// ['FormB']['FormB_Name'] = 'Name Submitted'
然后,您可以使用此結構來提交到數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.