提交多个同名字段到 php
Submit multiple fields of the same name to php
我想创建一个 table 用户可以填充值并提交到数据库。 table 的每一行都应作为单独的行保存在 table 中。 Table 示例如下所示。
First | Last | Phone
Steve | Jones | 1234
Jason | Smith | 4567
Mark | Black | 6789
Submit
我正在创建我的 table 作为表单并使用 POST 提交。如何仅使用 1 个提交按钮将每一行作为单独的行提交到我的数据库?
<input ... name="contact[1][first]">
<input ... name="contact[1][last]">
<input ... name="contact[1][phone]">
...
<input ... name="contact[2][first]">
<input ... name="contact[2][last]">
<input ... name="contact[2][phone]">
另请查看 this question。
P.S。你需要 google 更好。
<form>
<fieldset>
<input name="first[]" type="text"/>
....
可能对你有用,将字段名称作为数组可以让你迭代它们,例如:
<?php
$firsts = $_POST["first"];
$lasts = $_POST["lasts"];
$num = $_POST["numbers"];
if(count($firsts) == count($lasts) == count($num) {
for($i = 0; $i < count($firsts); $i++) {
$toSubmit = [$firsts[$i], $lasts[$i], $num[$i]];
// put that into your SQL inserts w/e.
}
}
?>
<form method='POST' action='submitPage.php'>
<table>
<tr>
<td><input name='first[]'></td>
<td><input name='last[]'></td>
<td><input name='phone[]'></td>
</tr>
<tr>
<td><input name='first[]'></td>
<td><input name='last[]'></td>
<td><input name='phone[]'></td>
</tr>
.
.
</table>
<input type='submit' value='submit details'>
</form>
submitPage.php
<?
$first = $_POST['first'];
$last = $_POST['last'];
$phone = $_POST['phone'];
$totalFirstName = sizeof($first);
for($i=0;$i<$totalFirstName;$i++)
{
$FirstName = $first[$i];
$LastName = $last[$i];
$Phone = $phone[$i];
$Query = "INSERT INTO TableName SET first='$FirstName', last='$LastName', phone='$Phone'";
//Execute Your Query
}
?>
我想创建一个 table 用户可以填充值并提交到数据库。 table 的每一行都应作为单独的行保存在 table 中。 Table 示例如下所示。
First | Last | Phone
Steve | Jones | 1234
Jason | Smith | 4567
Mark | Black | 6789
Submit
我正在创建我的 table 作为表单并使用 POST 提交。如何仅使用 1 个提交按钮将每一行作为单独的行提交到我的数据库?
<input ... name="contact[1][first]">
<input ... name="contact[1][last]">
<input ... name="contact[1][phone]">
...
<input ... name="contact[2][first]">
<input ... name="contact[2][last]">
<input ... name="contact[2][phone]">
另请查看 this question。
P.S。你需要 google 更好。
<form>
<fieldset>
<input name="first[]" type="text"/>
....
可能对你有用,将字段名称作为数组可以让你迭代它们,例如:
<?php
$firsts = $_POST["first"];
$lasts = $_POST["lasts"];
$num = $_POST["numbers"];
if(count($firsts) == count($lasts) == count($num) {
for($i = 0; $i < count($firsts); $i++) {
$toSubmit = [$firsts[$i], $lasts[$i], $num[$i]];
// put that into your SQL inserts w/e.
}
}
?>
<form method='POST' action='submitPage.php'>
<table>
<tr>
<td><input name='first[]'></td>
<td><input name='last[]'></td>
<td><input name='phone[]'></td>
</tr>
<tr>
<td><input name='first[]'></td>
<td><input name='last[]'></td>
<td><input name='phone[]'></td>
</tr>
.
.
</table>
<input type='submit' value='submit details'>
</form>
submitPage.php
<?
$first = $_POST['first'];
$last = $_POST['last'];
$phone = $_POST['phone'];
$totalFirstName = sizeof($first);
for($i=0;$i<$totalFirstName;$i++)
{
$FirstName = $first[$i];
$LastName = $last[$i];
$Phone = $phone[$i];
$Query = "INSERT INTO TableName SET first='$FirstName', last='$LastName', phone='$Phone'";
//Execute Your Query
}
?>