mikroavr

http_post_json

Jun 9th, 2021
829
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $json = file_get_contents('php://input');
  3. $data = json_decode($json);
  4. $room = $data->room;
  5. $polybag = $data->polybag;
  6. $lux_m = $data->lux_m;
  7. $moistur = $data->moistur;
  8. $suhu_m = $data->suhu_m;
  9. $suhu_g = $data->suhu_g;
  10. $hum_g = $data->hum_g;
  11. $lux_g = $data->lux_g;
  12. $lat = $data->lat;
  13. $lon = $data->lon;
  14. $_ts = $data->ts;
  15. $time = strtotime($_ts);
  16. $ts = date("Y-m-d H:i:s", $time);
  17.  
  18. $servername = "localhost";
  19. $username = "user";
  20. $password = "";
  21. $dbName = "tbl_kebun";
  22.  
  23. // Create connection
  24. $conn = mysqli_connect($servername, $username, $password, $dbName);
  25. // Check connection
  26. if (!$conn) {
  27.   die("Connection failed: " . mysqli_connect_error());
  28. }else{
  29. // echo "sukses to server";
  30. }
  31. if($room == 100){
  32. $sql = "INSERT INTO room1 (polybag, lux_m, moistur, suhu_m, suhu_g, hum_g, lux_g, lat, lon, time_g)
  33. VALUES ('$polybag', '$lux_m', '$moistur', '$suhu_m','$suhu_g', '$hum_g', '$lux_g', '$lat', '$lon', '$ts')";
  34. if (mysqli_query($conn, $sql)) {
  35.  
  36.      $sts_crud = '{"status":1}';
  37.      echo $sts_crud;
  38.   } else {
  39.      $sts_crud = '{"status":0}';
  40.      echo $sts_crud;
  41. }
  42.  
  43. mysqli_close($conn);
  44. }
  45. if($room == 101){
  46.         //echo "get room 101";
  47. $sql = "INSERT INTO room2 (polybag, lux_m, moistur, suhu_m, suhu_g, hum_g, lux_g, lat, lon, time_g)
  48. VALUES ('$polybag', '$lux_m', '$moistur', '$suhu_m','$suhu_g', '$hum_g', '$lux_g', '$lat', '$lon', '$ts')";
  49. if (mysqli_query($conn, $sql)) {
  50.  
  51.      $sts_crud = '{"status":1}';
  52.      echo $sts_crud;
  53.   } else {
  54.      $sts_crud = '{"status":0}';
  55.      echo $sts_crud;
  56. }
  57.  
  58. mysqli_close($conn);
  59. }
  60. if($room == 102){
  61.         //echo "get room 102";
  62.        //echo "get room 101";
  63. $sql = "INSERT INTO room3 (polybag, lux_m, moistur, suhu_m, suhu_g, hum_g, lux_g, lat, lon, time_g)
  64. VALUES ('$polybag', '$lux_m', '$moistur', '$suhu_m','$suhu_g', '$hum_g', '$lux_g', '$lat', '$lon', '$ts')";
  65. if (mysqli_query($conn, $sql)) {
  66.  
  67.      $sts_crud = '{"status":1}';
  68.      echo $sts_crud;
  69.   } else {
  70.      $sts_crud = '{"status":0}';
  71.      echo $sts_crud;
  72. }
  73.  
  74. mysqli_close($conn);
  75. }
  76.  
  77. ?>
  78.  
  79.  
  80.  
RAW Paste Data