Nav apraksta

deleteMoment.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. require_once 'config.php';
  3. require_once 'dbh.inc.php';
  4. require_once 'checkLogin.php';
  5. // EXAMPLE INPUT...
  6. // array(2) { ["momentID"]=> string(3) "137" ["deleteMoment"]=> string(0) "" }
  7. // WARNING: SHOULD CONFIRM THAT MOMENT BELONGS TO THE CORRECT USER (AUTHENTICATE THE TRANSACTION)
  8. if(isset($_POST['deleteMoment'])) {
  9. $momentID = mysqli_real_escape_string($connection, trim($_POST['momentID']));
  10. // Check that moment ID is not empty string
  11. if($momentID === "") {
  12. http_response_code(400);
  13. echo json_encode(array("error" => "Please specify moment ID."));
  14. exit();
  15. }
  16. // Check that moment is registered in the database
  17. if(mysqli_query($connection, "SELECT * FROM subquestionnair WHERE id = '$momentID';")->num_rows !== 1) {
  18. http_response_code(400);
  19. echo json_encode(array("error" => "Given moment ID ($momentID) not in database."));
  20. exit();
  21. }
  22. // Check that the moment hasn't been answered yet
  23. if(mysqli_query($connection, "SELECT * FROM student_subquestionnair WHERE id_subquestionnair = '$momentID';")->num_rows !== 0) {
  24. http_response_code(400);
  25. echo json_encode(array("error" => "Moment already active, deletion denied."));
  26. exit();
  27. }
  28. /*** STEPS ***/
  29. // DELETE MOMENT-QUESTION RELATIONS
  30. // DELETE EXPERIENCE-MOMENT RELATION
  31. // DELETE MOMENT
  32. // Delete moment-question relations
  33. $queryUnhookQuestionFromMoment = "DELETE FROM subquestionnair_question WHERE id_subquestionnair = '$momentID';";
  34. $result = mysqli_query($connection, $queryUnhookQuestionFromMoment) or die("Error: ".mysqli_error($connection));
  35. // Delete experience-moment relation
  36. $queryUnhookMomentFromExperience = "DELETE FROM experience_subquestionnair WHERE id_subquestionnair = '$momentID';";
  37. $result = mysqli_query($connection, $queryUnhookMomentFromExperience) or die("Error: ".mysqli_error($connection));
  38. // Delete moments
  39. $queryDeleteMoments = "DELETE FROM subquestionnair WHERE id = '$momentID';";
  40. $result = mysqli_query($connection, $queryDeleteMoments) or die("Error: ".mysqli_error($connection));
  41. }