暫無描述

putFoundFlower.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 putFoundFlower($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 = 1 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. $query = "INSERT INTO UserHistory (`user_id`, `flower_id`, `in_wishlist`, `has_been_found`) VALUES ('$userID', '$flowerID', 0, 1);";
  36. if ($connection->query($query)) {
  37. http_response_code(200);
  38. } else {
  39. http_response_code(500); // use appropriate status code
  40. echo json_encode(array("error" => $connection->error));
  41. }
  42. }
  43. } else {
  44. http_response_code(500); // use appropriate status code
  45. echo json_encode(array("error" => $connection->error));
  46. }
  47. }
  48. putFoundFlower($userID, $flowerID);