PHP Multiple Mysql插入脚本说明。怎么样?

时间:2018-03-22 00:26:27

标签: php jquery mysql json

我需要帮助理解这个聪明的PHP Multiple Mysql Insert代码。

请允许我强调一下,我发现在线解析JSON数据,剩下的就是我的。

它完美无缺但有些事情我还不完全了解......

  1. 如何构建插入字符串?如果你可以评论那些很棒的代码......

  2. 是重复插入还是一个巨大的插入PDO执行?

  3. 我注意到它使用bindValue而不是bindParameter。这是因为这个动态PHP脚本的性质吗?

  4. 可选:如果您知道一种简单明了的方法,请务必告诉我您是否有机会。

  5. JSON POST数据

    [
       {
          "PK_LINE_ITEM_ID":555,
          "DESCRIPTION":"LINE ITEM 5",
          "QUANTITY":0,
          "UNIT":"SF",
          "COST":0,
          "TOTAL":"0.00"
       },
       {
          "PK_LINE_ITEM_ID":777,
          "DESCRIPTION":"LINE ITEM 7",
          "QUANTITY":0,
          "UNIT":"SF",
          "COST":0,
          "TOTAL":"0.00"
       },
       {
          "PK_LINE_ITEM_ID":999,
          "DESCRIPTION":"LINE ITEM 9",
          "QUANTITY":0,
          "UNIT":"SF",
          "COST":0,
          "TOTAL":"0.00"
       }
    ]
    

    PHP脚本(data_post_json_insert_all.php

    <?php
    /* Status Codes
    
    return 0 = Nothing to Update (n/a)
    return 1 = Successful Insert Query
    return 2 = Database Connection refused
    return 3 = MySQL Query Error OR Wrong URL Parameters */
    
    /* Disable Warnings so that we can return ONLY what we want through echo. */
    mysqli_report(MYSQLI_REPORT_STRICT);
    
    // First get raw POST input
    $raw_post = file_get_contents('php://input');
    // Run through url_decode..
    $url_decoded = urldecode($raw_post);
    // Run through json_decode...
    // false to allow for reference to oject. eg. $column->name instead of $column["name"] in the foreach.
    $json_decoded = json_decode($url_decoded, true);
    
    $table_name = 'tbl_xyz';
    
    // INCLUDE DB CONNECTION STRING
    include 'php_pdo_mysql_connect.php';
    
    pdoMultiInsert($table_name, $json_decoded, $link);
    
    function pdoMultiInsert($mysql_table, $json_decoded, $pdo_object) {
    
        //Will contain SQL snippets.
        $rows_sql = [];
    
        //Will contain the values that we need to bind.
        $to_bind = [];
    
        //Get a list of column names to use in the SQL statement.
        $column_names = array_keys($json_decoded[0]);
    
    
        //Loop through our $json_decoded array.
        // begin outter for each
        foreach($json_decoded as $array_index => $row) {
    
            $params = [];
    
    
            // begin inner for each --------------------------------
            foreach($row as $column_name => $column_value) {
    
                $param = ":" . $column_name . $array_index;
    
                $params[] = $param;
    
                $to_bind[$param] = $column_value;
    
            } // end inner for each --------------------------------
    
    
            $rows_sql[] = "(" . implode(", ", $params) . ")";
    
        } // end outter for each
    
    
        //Construct our SQL statement
        $sql = "INSERT INTO `$mysql_table` (" . implode(", ", $column_names) . ") VALUES " . implode(", ", $rows_sql);
    
        //Prepare our PDO statement.
        $pdo_statement = $pdo_object->prepare($sql);
    
    
        //Bind our values.
        foreach($to_bind as $param => $val) {
            $pdo_statement->bindValue($param, $val);
        }
    
        //Execute our statement (i.e. insert the json_decoded data).
        return $pdo_statement->execute();
    }
    
    $link = null;
    $stmt = null;
    
    // return 1 = Successful Insert Query
    echo '1'; 
    

    由于

1 个答案:

答案 0 :(得分:1)

1)该脚本使用二维数组,以便更容易地准备插入查询。

它将为每一行创建一个数组(使用列名作为索引,使用字段值作为值),然后是包含这些行的第二个数组。因此,数组表示将要插入的所有数据,与应该包含的数据完全相同。

然后,它们使用昏迷作为胶水来破坏每一行 - 所以你将每个值用昏迷分隔并在其上加上括号。然后再次使用昏迷作为胶水内爆第二个数组,将挂载整个插入值查询。

2)执行在任何重复循环之外运行,因此它只是一个巨大的插入。

3)bindParam会将查询绑定到变量,如果此变量将来发生变化,它也会在查询中发生变化。 bindValue在运行时追加最终值。 Take a look at this thread.

4)这个脚本是通用的,可以使用不同的表设置 - 这是一个很好的方法。