Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "practice";
  6.  
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. if( $conn->connect_error ) die("Connection failed: " . $conn->connect_error);
  9. $payload_dump = $_POST['payload'];
  10.  
  11. $payload_array = json_decode($payload_dump,true);
  12.  
  13. if( is_array( $payload_array ) ){
  14.  
  15. $queries=array();
  16.  
  17. foreach( $payload_array as $row ){
  18. //get the data_payload details
  19. $device = $row['device'];
  20. $type = $row['data_type'];
  21. $zone = $row['zone'];
  22. $sample = $row['sample'];
  23. $count = $row['count'];
  24. $time = $row['date_time'];
  25. $epoch = $row['epoch_stamp'];
  26.  
  27. /*note: we do not need to add the semi-colon here as it gets added later when we implode the array */
  28. $queries[]="INSERT INTO `data` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";
  29. }
  30. /*
  31. Previously the below query was being execute on every iteration
  32. ~ because $epoch is now the last one encountered in the array,
  33. the value that is updated in ALL records is as it would have been
  34. previously.
  35. */
  36. $queries[]="UPDATE `data` SET `date_time` = from_unixtime( $epoch ) WHERE date_time = 0;";
  37.  
  38. $sql=implode( ';', $queries );
  39. if ( $conn->multi_query( $sql ) === TRUE ) {
  40. echo "New records created and updated successfully";
  41. } else {
  42. echo "Error: " . $sql . "<br>" . $conn->error;
  43. }
  44. }
  45. $conn->close();
  46.  
  47. ?>
  48.  
  49. <?php
  50. $servername = "localhost";
  51. $username = "root";
  52. $password = "";
  53.  
  54. // Create connection
  55. $conn = new mysqli($servername, $username, $password);
  56. if( $conn->connect_error ) die("Connection failed: " . $conn->connect_error);
  57.  
  58. ?>
  59.  
  60. <?php
  61. include 'dbconnect.php';
  62.  
  63. $payload_dump = $_POST['payload'];
  64.  
  65. $payload_array = json_decode($payload_dump,true);
  66.  
  67. if( is_array( $payload_array ) ){
  68.  
  69. $queries=array();
  70.  
  71. foreach( $payload_array as $row ){
  72. //get the data_payload details
  73. $device = $row['device'];
  74. $type = $row['data_type'];
  75. $zone = $row['zone'];
  76. $sample = $row['sample'];
  77. $count = $row['count'];
  78. $time = $row['date_time'];
  79. $epoch = $row['epoch_stamp'];
  80.  
  81. //$queries[]="INSERT INTO `data` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";
  82. $queries[]="INSERT INTO `data_test` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";
  83.  
  84. }
  85.  
  86. //$queries[]="UPDATE `data` SET `date_time` = from_unixtime( $epoch ) WHERE date_time = 0;";
  87. $queries[]="UPDATE `data_test` SET `date_time` = from_unixtime( $epoch ) WHERE date_time = 0;";
  88.  
  89. $sql=implode( ';', $queries );
  90. if ( $conn->multi_query( $sql ) === TRUE ) {
  91. echo "New records created and updated successfully";
  92. } else {
  93. echo "Error: " . $sql . "<br>" . $conn->error;
  94. }
  95. }
  96. $conn->close();
  97.  
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement