【问题标题】:Posting multiples dynamically created inputs and inserting them into a MySQL database发布多个动态创建的输入并将它们插入 MySQL 数据库
【发布时间】:2014-07-21 18:28:03
【问题描述】:

我有一个使用 jquery 动态创建的表。现在我将输入(当然是数组)发送到数据库。这些字段是customCLASSE[]customQTY[]customPRICE[]...这是我在 MySQL 中插入数据的脚本:

    if ($_POST['customCLASSE']) {
        foreach ( $_POST['customCLASSE'] as $key=>$value  ) {
           $query  = $mysqli->query("INSERT INTO booking_multiday_detail (classe) VALUES ('$value')");
        }
    } 

效果很好,但我的问题是:如何在插入 customCLASSE 的同时在同一个 foreach 中插入其他输入(customQTY[]customPRICE[]...)?

【问题讨论】:

  • 你不能对多个数组使用一个 foreach .. 尝试使用 'for' 方式然后(设置一个迭代器变量并迭代它).. 也使用 sql multi insert(在其中插入多行单个查询)

标签: php jquery html mysql


【解决方案1】:

$key 是循环中数组的索引。

因此,如果您的其他数组和 customCLASSE 一样排序,您可以像这样通过 $key 访问其他数组的值,

if ($_POST['customCLASSE']) {
        foreach ( $_POST['customCLASSE'] as $key=>$value  ) {
           $customQty = $_POST['customQTY'][$key];
           $customPRICE= $_POST['customQTY'][$key];
           //change the query
           //....
           $query  = $mysqli->query("INSERT INTO booking_multiday_detail (classe) VALUES ('$value')");
        }
    } 

【讨论】:

  • 好@ocanal!谢谢!我正在尝试一些非常相似的东西,它也适用于你的。我没有使用 $key,而是创建了一个名为 counter 的变量,它在循环中增加一,指示我想在数据库表中添加的数组项的确切数量。谢谢!
猜你喜欢
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 2023-04-10
相关资源
最近更新 更多