dourou9477 2014-09-08 14:24
浏览 60
已采纳

如何使用mysqli在数据库中插入动态多维数组

I have the following array:

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
)

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.

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).

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)

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongxianji0968 2014-09-10 11:19
    关注

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化