如何插入来自多个$ _ POST阵列排MYSQL [英] How to insert into MYSQL row from multiple $_POST arrays

查看:119
本文介绍了如何插入来自多个$ _ POST阵列排MYSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个添加联系人部分,其中在一点击,它增加了另一行与3个输入框形状的形状。 (上jfiddle的code段: http://jsfiddle.net/fmdx/cYxYP/

I have a form with an "add contact" section, where upon a click, it adds another row to the form with 3 more input boxes. (The segment of code on 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的插入:

With this being the PHP/MYSQL for insertion:

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();
}

在数据库中的表是:

The table in the database is:

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

我将如何扩大目前的MYSQL code从其他2阵列和输入他们的数据在同一个MYSQL一行循环?

How would I expand the current MYSQL code to accept entries from the other 2 arrays and input their data into the same MYSQL row during that loop?

推荐答案

您可以使用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";
}

这篇关于如何插入来自多个$ _ POST阵列排MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆