Php 如何一次在表中插入多行
我有一个MySQL数据库和php表单 当用户提交其详细信息时,我已成功更新到MySQL数据库 现在我有一个问题 我有5个表单,在同一个网页上有相同的字段。现在,用户提交所有5个表单并单击submit按钮,然后将详细信息保存到MySQL数据库中 我知道如果用户提交单个表单如何保存到数据库中的代码。但我需要提交具有相同字段的多个表单如何保存在数据库中 如果用户提交单个表单,则我的代码如下所示Php 如何一次在表中插入多行,php,mysql,Php,Mysql,我有一个MySQL数据库和php表单 当用户提交其详细信息时,我已成功更新到MySQL数据库 现在我有一个问题 我有5个表单,在同一个网页上有相同的字段。现在,用户提交所有5个表单并单击submit按钮,然后将详细信息保存到MySQL数据库中 我知道如果用户提交单个表单如何保存到数据库中的代码。但我需要提交具有相同字段的多个表单如何保存在数据库中 如果用户提交单个表单,则我的代码如下所示 <? if( $_POST ) { $con = mysql_connect("xx","
<?
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>
您不应同时提交多个表单。我能想到的唯一方法就是使用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();
}
//正在获取连接。。。
$conn=$this->_db->getConn();
对于($a=0;$aprepare('Insert in reservation(name,idUser,text))值('a','132','cccc'));
$query->execute();
}
您应该用特殊名称命名每个“表单”输入。其中一个方法是在每个输入后面加上一个数字,以便它检测您所谈论的表单
然后,只需使用逗号连接多个值
:
<?
// 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);
}
您一次只能从一个页面提交一个表单
您需要为表单元素提出一个命名方案,以便在提交表单元素时可以提取各个表单元素
像这样给出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>
然后,您可以使用此结构提交到数据库。为什么要提交具有相同信息的多个表单?您的具体问题是什么?列举每个实例(例如,firstname[]
)然后使用for
循环遍历发布的值如果它们是单独的表单,则需要使用Javascript通过Ajax提交所有表单。否则,将所有字段放在具有不同字段名称的同一表单上。@但是一次只能提交一个表单……但他可以将信息存储在po中st被称为“不同形式”只使用一个。这完全取决于变量名。如果你看到了,我没有告诉你任何关于多个表单的事情,但是多个值你希望他知道PDO,他的系统已经准备好使用它。尽管他会使用这个,但这并不是用你自己的复制粘贴跳过他的例子的借口。PS:你正在准备一个查询。如果完成了很多时候,您可能只准备一次,然后在execute()
中传递参数。这只是一个关于如何完成它的示例。(正如我指出的)。我不想鼓励使用mysql\u query
,因为它不再安全了,而且他的查询太大,无法用简单的方式说明过程。PS:那是个错误:)mysql\u query
从来都不安全,只是提醒一下。但我明白你的意思。无论如何,他可以逃避每一个输入。顺便说一下,请再次检查我的回答和建议。
//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'