如何插入动态多维数组与mysqli的数据库 [英] How to insert dynamic multidimensional array in database with mysqli

查看:186
本文介绍了如何插入动态多维数组与mysqli的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

Array
(
    [step1] => 3
    [step2] => Array
        (
            [1] => Array
                (
                    [type] => 2
                    [price] => 312.5
                )

            [0] => Array
                (
                    [type] => 1
                    [price] => 51.5
                )

        )

    [step3] => Array
        (
            [first_name] => Test
            [last_name] => Test
        )

    [step4] => Some answer
)

,我想在数据库中插入。作为用户希望第二步可以有很多的项目。正如你可能已经注意到有2个阵列步骤2的之间的差异。第三步也可以有几个领域。这是所有用户依赖。

Which I would like to Insert in a database. Step2 can have as many items as the user wants. As you might have noticed there are differences between the 2 arrays of step2. Also step3 can have a few more fields. This is all user dependent.

原因它是动态的,因为用户可以在仪表盘管理表单的输入域。

The reason it is dynamic is because a user can manage form input fields in a dashboard.

我试图做的每一步插入但这并没有工作非常好foreach循环。我希望做的是:有一个查询,它是动态的,因此每个字段可以在数据库中插入。我认为,要做到这一点最简单的方法就是做第二步foreach循环使每个项目具有其它值。我的问题是我怎么能插入来自数据库中的这个多维数组中的所有数据与mysqli的(OOP)。

I tried to do a foreach loop for each step to insert but this didn't work really well. What I would like to do is the following: Have a query which is dynamic so each field can be inserted in the database. I think the easiest way to do this is to do a foreach loop on step2 so each item has the other values as well. My question is how can I insert all the data from this multidimensional array in a database with mysqli (OOP).

还检查了这样像一些其他问题:
<一href=\"http://stackoverflow.com/questions/19463107/inserting-data-into-mysql-from-a-multidimensional-array-in-php\">Inserting从PHP 多维数组数据到MySQL,
<一href=\"http://stackoverflow.com/questions/779986/insert-multiple-rows-via-a-php-array-into-mysql\">insert通过PHP数组多行到mysql ,
插入PHP数组到mysql
但这些不帮我。因此,破灭()是不会帮助我。也序列化是行不通的,因为每个领域在数据库中其自己的领域。 (当用户编辑在数据库中的仪表板领域的表单还得到改变)

Also checked a few other questions like this like: Inserting data into MySQL from a multidimensional array in php, insert multiple rows via a php array into mysql, insert php array into mysql But these don't help me. So implode() is not going to help me. Also serialize is not going to work since each field has its own field in the database. (When a user edits the form in the dashboard fields in the database also get changed)

在此先感谢

推荐答案

我结束了创建第二个表容纳所有订单的详细信息。其他表我编辑的,因此它可以容纳所有的用户数据。 Orders表中也有一个用户id链接。

I ended up creating a second table to house all the order details. The other table I edited so it can hold all the user data. The orders table also has an userId to link.

然后,我创建了一个功能,插入所有用户的数据:

I then created a function to insert all the user data:

function insertUserData($array, $table) {

   $tbl_fields = $this->tableFields($table); 

    $query = "INSERT INTO `user_orders` ("; // Query
    foreach($array as $field => $value) {  
       if(array_key_exists($field, $tbl_fields)) {
            $query .= "`$field`, ";
       }
    }

    $query .= ") VALUES ("; // Add to query
    foreach($array as $field => $value) {  
       if(array_key_exists($field, $tbl_fields)) {
            $query .= "'$value', ";
       }
    }

    $query .=")"; // End 
    //echo $query; //Query output

    if($db->mysqli->query($query)) {
        $insert_id = $db->mysqli->insert_id;
    } else {
        echo 'Something went wrong';
    }
    return $insert_id;
}

和第2步,我可以做到以下几点:

And for step 2 I could do the following:

foreach($data['stap2'] as $key => $array) {
        $query = "INSERT INTO `orders` (";
        foreach($array as $field => $value) {
            if(array_key_exists($field, $table)) {
                $query .= "`$field`, ";
            }
        } 

        $query .= "`order_id`) VALUES (";

        foreach($array as $field => $value) {
            if(array_key_exists($field, $table)) {
                $query .= "'$value', ";
            }
        }
        $query .= "'$insert_id')";
        echo $query.'<br>';
    }

我检查每个字段从表中的字段。如果它们相等它们添加到查询中。这样,我创建了一个动态查询。
这解决了我的问题。

I check each field to the fields from the table. If a they are equal add them to the query. This way I create a dynamic query. This solved the problem for me.

这篇关于如何插入动态多维数组与mysqli的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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