Guest User

Untitled

a guest
Mar 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. [
  2. {
  3. "PK_LINE_ITEM_ID":555,
  4. "DESCRIPTION":"LINE ITEM 5",
  5. "QUANTITY":0,
  6. "UNIT":"SF",
  7. "COST":0,
  8. "TOTAL":"0.00"
  9. },
  10. {
  11. "PK_LINE_ITEM_ID":777,
  12. "DESCRIPTION":"LINE ITEM 7",
  13. "QUANTITY":0,
  14. "UNIT":"SF",
  15. "COST":0,
  16. "TOTAL":"0.00"
  17. },
  18. {
  19. "PK_LINE_ITEM_ID":999,
  20. "DESCRIPTION":"LINE ITEM 9",
  21. "QUANTITY":0,
  22. "UNIT":"SF",
  23. "COST":0,
  24. "TOTAL":"0.00"
  25. }
  26. ]
  27.  
  28. <?php
  29. /* Status Codes
  30.  
  31. return 0 = Nothing to Update (n/a)
  32. return 1 = Successful Insert Query
  33. return 2 = Database Connection refused
  34. return 3 = MySQL Query Error OR Wrong URL Parameters */
  35.  
  36. /* Disable Warnings so that we can return ONLY what we want through echo. */
  37. mysqli_report(MYSQLI_REPORT_STRICT);
  38.  
  39. // First get raw POST input
  40. $raw_post = file_get_contents('php://input');
  41. // Run through url_decode..
  42. $url_decoded = urldecode($raw_post);
  43. // Run through json_decode...
  44. // false to allow for reference to oject. eg. $column->name instead of $column["name"] in the foreach.
  45. $json_decoded = json_decode($url_decoded, true);
  46.  
  47. $table_name = 'tbl_xyz';
  48.  
  49. // INCLUDE DB CONNECTION STRING
  50. include 'php_pdo_mysql_connect.php';
  51.  
  52. pdoMultiInsert($table_name, $json_decoded, $link);
  53.  
  54. function pdoMultiInsert($mysql_table, $json_decoded, $pdo_object){
  55.  
  56. //Will contain SQL snippets.
  57. $rows_sql = array();
  58.  
  59. //Will contain the values that we need to bind.
  60. $to_bind = array();
  61.  
  62. //Get a list of column names to use in the SQL statement.
  63. $column_names = array_keys($json_decoded[0]);
  64.  
  65.  
  66. //Loop through our $json_decoded array.
  67. // begin outter for each
  68. foreach($json_decoded as $array_index => $row){
  69.  
  70. $params = array();
  71.  
  72.  
  73. // begin inner for each --------------------------------
  74. foreach ($row as $column_name => $column_value) {
  75.  
  76. $param = ":" . $column_name . $array_index;
  77.  
  78. $params[] = $param;
  79.  
  80. $to_bind[$param] = $column_value;
  81.  
  82. } // end inner for each --------------------------------
  83.  
  84.  
  85. $rows_sql[] = "(" . implode(", ", $params) . ")";
  86.  
  87. } // end outter for each
  88.  
  89.  
  90. //Construct our SQL statement
  91. $sql = "INSERT INTO `$mysql_table` (" . implode(", ", $column_names) . ") VALUES " . implode(", ", $rows_sql);
  92.  
  93. //Prepare our PDO statement.
  94. $pdo_statement = $pdo_object->prepare($sql);
  95.  
  96.  
  97. //Bind our values.
  98. foreach ($to_bind as $param => $val) {
  99.  
  100. $pdo_statement->bindValue($param, $val);
  101.  
  102. }
  103.  
  104. //Execute our statement (i.e. insert the json_decoded data).
  105. return $pdo_statement->execute();
  106.  
  107. }
  108.  
  109. $link = null;
  110. $stmt = null;
  111.  
  112. // return 1 = Successful Insert Query
  113. echo '1';
  114.  
  115. ?>
Add Comment
Please, Sign In to add comment