No Description

removeQuestionFromMoment.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. require_once 'config.php';
  3. require_once 'dbh.inc.php';
  4. require_once 'checkLogin.php';
  5. // EXAMPLE INPUT...
  6. // array(3) { ["momentID"]=> string(3) "137" ["questionID"]=> string(3) "266" ["removeQuestionFromMoment"]=> string(0) "" }
  7. if(isset($_POST['removeQuestionFromMoment'])) {
  8. $momentID = mysqli_real_escape_string($connection, trim($_POST['momentID']));
  9. $questionID = mysqli_real_escape_string($connection, trim($_POST['questionID']));
  10. // Check that moment ID is not empty string
  11. // And that it's registered in the database
  12. if($momentID === "") {
  13. http_response_code(400);
  14. echo json_encode(array("error" => "Please specify moment ID."));
  15. exit();
  16. } else if(mysqli_query($connection, "SELECT * FROM subquestionnair WHERE id = '$momentID';")->num_rows !== 1) {
  17. http_response_code(400);
  18. echo json_encode(array("error" => "Given moment ID ($momentID) not in database."));
  19. exit();
  20. }
  21. // Check that question ID is not empty string
  22. // And that it's registered in the database
  23. // And that it belongs to the Questionnaire the Moment belongs to
  24. // And that it isn't a duplicate inside the Moment
  25. if($questionID === "") {
  26. http_response_code(400);
  27. echo json_encode(array("error" => "Please specify question ID."));
  28. exit();
  29. } else if(mysqli_query($connection, "SELECT * FROM question WHERE id = '$questionID';")->num_rows !== 1) {
  30. http_response_code(400);
  31. echo json_encode(array("error" => "Given question ID ($questionID) not in database."));
  32. exit();
  33. } else if(mysqli_query($connection, "SELECT * FROM question WHERE id = '$questionID' AND id IN (SELECT id_question FROM questionnair_question WHERE id_questionnair = (SELECT id_questionnair FROM subquestionnair WHERE id = '$momentID'));")->num_rows !== 1) {
  34. http_response_code(400);
  35. echo json_encode(array("error" => "Given question ID ($questionID) is outside the Moment's corresponding Questionnair's scope."));
  36. exit();
  37. } else if(mysqli_query($connection, "SELECT * FROM subquestionnair_question WHERE id_question = '$questionID' AND id_subquestionnair = '$momentID';")->num_rows === 0) {
  38. http_response_code(400);
  39. echo json_encode(array("error" => "Question is already removed from moment."));
  40. exit();
  41. }
  42. // Remove question from moment
  43. $query = "DELETE FROM subquestionnair_question WHERE id_subquestionnair = '$momentID' AND id_question = '$questionID';";
  44. $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
  45. }