Без опису

deleteQuestion.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. require_once 'config.php';
  3. require_once 'dbh.inc.php';
  4. require_once 'checkLogin.php';
  5. // EXAMPLE INPUT...
  6. // array(2) { ["questionID"]=> string(3) "267" ["deleteQuestion"]=> string(0) "" }
  7. // WARNING: SHOULD CONFIRM THAT QUESTION BELONGS TO THE CORRECT USER (AUTHENTICATE THE TRANSACTION)
  8. // WARNING: IF USER DELETES ALL QUESTIONS FROM A QUESTIONNAIRE USING THIS METHOD, THE DATABASE WILL HAVE UNUSED CATEGORIES/SUBCATEGORIES, AS THEY ARE NOT ASSOCIATED DIRECTLY TO THE QUESTIONNAIRE
  9. // var_dump($_POST);
  10. // exit();
  11. if(isset($_POST['deleteQuestion'])) {
  12. $questionID = mysqli_real_escape_string($connection, trim($_POST['questionID']));
  13. // Check that question ID is not empty string
  14. if($questionID === "") {
  15. http_response_code(400);
  16. echo json_encode(array("error" => "Please specify question ID."));
  17. exit();
  18. }
  19. // Check that question is registered in the database
  20. if(mysqli_query($connection, "SELECT * FROM question WHERE id = '$questionID';")->num_rows !== 1) {
  21. http_response_code(400);
  22. echo json_encode(array("error" => "Given question ID ($questionID) not in database."));
  23. exit();
  24. }
  25. // Check that question hasn't been answered yet
  26. if(mysqli_query($connection, "SELECT * FROM answer WHERE id_question = '$questionID';")->num_rows !== 0) {
  27. http_response_code(400);
  28. echo json_encode(array("error" => "Question has already been answered by one or more people, deletion denied."));
  29. exit();
  30. }
  31. /*** STEPS ***/
  32. // DELETE MOMENT-QUESTION RELATION
  33. // DELETE QUESTIONNAIRE-QUESTION RELATION
  34. // DELETE QUESTION-TYPES
  35. // DELETE QUESTION
  36. // Delete moment-question relation
  37. $queryUnhookQuestionFromMoment = "DELETE FROM subquestionnair_question WHERE id_question = '$questionID';";
  38. $result = mysqli_query($connection, $queryUnhookQuestionFromMoment) or die("Error: ".mysqli_error($connection));
  39. // Delete questionnaire-question relation
  40. $queryUnhookQuestionFromQuestionnaire = "DELETE FROM questionnair_question WHERE id_question = '$questionID';";
  41. $result = mysqli_query($connection, $queryUnhookQuestionFromQuestionnaire) or die("Error: ".mysqli_error($connection));
  42. // Delete question types
  43. $queryDeleteQuestionTypes = "DELETE FROM question_type WHERE id_question = '$questionID';";
  44. $result = mysqli_query($connection, $queryDeleteQuestionTypes) or die("Error: ".mysqli_error($connection));
  45. // Delete question
  46. $queryDeleteQuestion = "DELETE FROM question WHERE id = '$questionID';";
  47. $result = mysqli_query($connection, $queryDeleteQuestion) or die("Error: ".mysqli_error($connection));
  48. }