Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- {
- "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
- /* 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 = array();
- //Will contain the values that we need to bind.
- $to_bind = array();
- //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 = array();
- // 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';
- ?>
Add Comment
Please, Sign In to add comment