1

我有一个带有“添加联系人”部分的表单,点击后,它会在表单中添加另一行,其中包含另外 3 个输入框。(jfiddle上的代码段:http: //jsfiddle.net/fmdx/cYxYP/

HTML

<form method="post" action="">
<div style="padding-left:40px;">
<div id="customcopies" style="padding-left:40px;">
1. Name: <input type="text" id="copiestoname_1" name="copiestoname[]" placeholder="Jane Doe Smith" required>, Institution: <input type="text" id="copiestoinst_1" name="copiestoinst[]" placeholder="Bank" required>, Method: <input type="text" id="copiestomethod_1" name="copiestomethod[]" placeholder="Email" required>
</div>
</div>
<input type="submit" name="submit_val" value="Submit" />
</form>

这是用于插入的 PHP/MYSQL:

if (isset($_POST['submit_val'])) {
    foreach ($_POST['copiestoname'] as $key=>$value) {
    $copiestoname = mysql_real_escape_string($value);

        mysql_query("INSERT INTO copiesto (name) VALUES ('$copiestoname')") or die(mysql_error());
        echo "Completed";
    }
echo "" . count($_POST['copiestoname']) . " Names Added<br>";
mysql_close();
}

数据库中的表是:

Table Name: copiesto
+-------------+-------------+-------------+---------+
| index       |  name       | institution | method  |
+-------------+-------------+-------------+---------+

我将如何扩展当前的 MYSQL 代码以接受来自其他 2 个数组的条目并在该循环期间将它们的数据输入到同一 MYSQL 行中?

4

1 回答 1

4

你可以使用 for 循环

for ($i=0; $i < count($_POST['copiestoname']); $i++ ) {
  $copiestoname = mysql_real_escape_string($_POST['copiestoname'][$i]);
  $copiestoinst = mysql_real_escape_string($_POST['copiestoinst'][$i]);
  $copiestomethod = mysql_real_escape_string($_POST['copiestomethod'][$i]);

  mysql_query("INSERT INTO copiesto (name, institution, method) VALUES ('$copiestoname', '$copiestoinst', '$copiestomethod')") or die(mysql_error());
  echo "Completed";
}
于 2013-09-06T19:45:48.010 回答