发布多次动态创建的输入并将其插入MySQL数据库

时间:2014-06-01 14:37:28

标签: php jquery html mysql

我有一个使用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的同一时间内将其他输入(customQTY[]customPRICE[] ...)插入同一个foreach中?

1 个答案:

答案 0 :(得分:0)

$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')");
        }
    } 
相关问题