1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
-
- require_once 'config.php';
- require_once 'dbh.inc.php';
- require_once 'checkLogin.php';
-
-
- // EXAMPLE INPUT...
- // array(2) { ["questionID"]=> string(3) "267" ["deleteQuestion"]=> string(0) "" }
-
- // WARNING: SHOULD CONFIRM THAT QUESTION BELONGS TO THE CORRECT USER (AUTHENTICATE THE TRANSACTION)
- // 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
-
- // var_dump($_POST);
- // exit();
-
-
- if(isset($_POST['deleteQuestion'])) {
-
-
-
- $questionID = mysqli_real_escape_string($connection, trim($_POST['questionID']));
-
-
- // Check that question ID is not empty string
- if($questionID === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify question ID."));
- exit();
- }
-
-
- // Check that question is registered in the database
- if(mysqli_query($connection, "SELECT * FROM question WHERE id = '$questionID';")->num_rows !== 1) {
- http_response_code(400);
- echo json_encode(array("error" => "Given question ID ($questionID) not in database."));
- exit();
- }
-
-
- // Check that question hasn't been answered yet
- if(mysqli_query($connection, "SELECT * FROM answer WHERE id_question = '$questionID';")->num_rows !== 0) {
- http_response_code(400);
- echo json_encode(array("error" => "Question has already been answered by one or more people, deletion denied."));
- exit();
- }
-
-
-
- /*** STEPS ***/
-
- // DELETE MOMENT-QUESTION RELATION
- // DELETE QUESTIONNAIRE-QUESTION RELATION
- // DELETE QUESTION-TYPES
- // DELETE QUESTION
-
-
-
- // Delete moment-question relation
- $queryUnhookQuestionFromMoment = "DELETE FROM subquestionnair_question WHERE id_question = '$questionID';";
- $result = mysqli_query($connection, $queryUnhookQuestionFromMoment) or die("Error: ".mysqli_error($connection));
-
- // Delete questionnaire-question relation
- $queryUnhookQuestionFromQuestionnaire = "DELETE FROM questionnair_question WHERE id_question = '$questionID';";
- $result = mysqli_query($connection, $queryUnhookQuestionFromQuestionnaire) or die("Error: ".mysqli_error($connection));
-
- // Delete question types
- $queryDeleteQuestionTypes = "DELETE FROM question_type WHERE id_question = '$questionID';";
- $result = mysqli_query($connection, $queryDeleteQuestionTypes) or die("Error: ".mysqli_error($connection));
-
- // Delete question
- $queryDeleteQuestion = "DELETE FROM question WHERE id = '$questionID';";
- $result = mysqli_query($connection, $queryDeleteQuestion) or die("Error: ".mysqli_error($connection));
-
-
-
- }
|