Keine Beschreibung

removeFoundFlower.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. require_once('config.php');
  3. // Filter unsupported HTTP requests
  4. if ($_SERVER["REQUEST_METHOD"] !== "POST") {
  5. http_response_code(400); // use appropriate status
  6. echo json_encode(array("error" => "Endpoint only supports POST requests"));
  7. exit();
  8. }
  9. // Get input
  10. $json = file_get_contents('php://input');
  11. $data = json_decode($json);
  12. $userID = isset($data->user_id) ? $data->user_id : '';
  13. $flowerID = isset($data->flower_id) ? $data->flower_id : '';
  14. // Clean user input
  15. $flowerID = $connection->real_escape_string($flowerID);
  16. $userID = $connection->real_escape_string($userID);
  17. function removeFoundFlower($userID, $flowerID) {
  18. if ($userID == '' || $flowerID == '') {
  19. http_response_code(400); // use appropriate status code
  20. echo json_encode(array("error" => "Fields 'user_id' and 'flower_id' are requried"));
  21. return;
  22. }
  23. global $connection;
  24. $query = "SELECT * FROM UserHistory AS H WHERE H.user_id = '$userID' AND H.flower_id = '$flowerID';";
  25. if ($result = $connection->query($query)) {
  26. if ($result->num_rows > 0) {
  27. $query = "UPDATE UserHistory AS H SET H.has_been_found = 0 WHERE H.user_id = '$userID' AND H.flower_id = '$flowerID';";
  28. if ($connection->query($query)) {
  29. http_response_code(200);
  30. } else {
  31. http_response_code(500); // use appropriate status code
  32. echo json_encode(array("error" => $connection->error));
  33. }
  34. } else {
  35. // Theres nothing to remove, since it doesn't exist
  36. http_response_code(200);
  37. }
  38. } else {
  39. http_response_code(500); // use appropriate status code
  40. echo json_encode(array("error" => $connection->error));
  41. }
  42. }
  43. // mail("vhernandezcastro@gmail.com", "test_remove_favorite_android", json_encode($data));
  44. removeFoundFlower($userID, $flowerID);