暫無描述

insertQuestionnaire.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <!---
  2. Developer: Coralys Cubero Rivera
  3. Fall 2018
  4. This scripts inserts a new questionnaire into our system, including each of its questions.
  5. Updated by: Víctor A. Hernández
  6. Summer 2019
  7. --->
  8. <?php
  9. require_once 'processes/config.php';
  10. require_once 'processes/dbh.inc.php';
  11. // IF USER ENTERS PAGE AFTER SUBMIT BUTTON HAS BEEN PRESSED (FROM newQuestionnaire.php), INSERT QUESTIONNAIRE TO DATABASE
  12. // ELSE DO NOTHING
  13. if(isset($_POST['submitQuestionnaire'])) {
  14. // RECIBIR TODOS LOS INPUTS DE newQuestionnaire.php (TÍTULO, DESCRIPCIÓN Y CADA PREGUNTA A AÑADIRSE AL CUESTIONARIO)
  15. $keys = array();
  16. $values = array();
  17. foreach($_POST as $key => $value) {
  18. $keys[] = $key;
  19. $values[] = $value;
  20. }
  21. // INSERTAR A TABLA questionair EL TÍTULO Y LA DESCRIPCIÓN DEL CUESTIONARIO NUEVO
  22. $q_title = $values[0]; // usar POST['q_title'];
  23. $q_description = $values[1]; // usar POST['q_description'];
  24. $query = "INSERT INTO `questionnair` (`title`, `description`) VALUES ('".$q_title."', '".$q_description."');";
  25. if(!mysqli_query($connection, $query)) die("Error: ".mysqli_error($connection));
  26. // BUSCAR id DEL CUESTIONARIO ACABADO DE INSERTAR
  27. $query = "SELECT `id` FROM `questionnair` WHERE `title`='".$q_title."';";
  28. $result = mysqli_query($connection, $query);
  29. if(!$result) die("Error: ".mysqli_error($connection));
  30. //$row = mysqli_fetch_array($result);
  31. //$id_questionnaire = $row[0];
  32. foreach($result as $questionnaire) $questionnaire_id = $questionnaire["id"];
  33. // "INSERTAR" CADA PREGUNTA DESEADA AL CUESTIONARIO NUEVO (ASOCIANDO LOS ids A TRAVÉS DE LA TABLA questionnair_question)
  34. // WARNING: ESTO NO PROVEE PROTECCIÓN POR SI EL VALUE DEL SELECT ES '' (PREGUNTA INVÁLIDA) O SI HAY UNA PREGUNTA REPETIDA
  35. $num_questions = sizeof($values);
  36. for($x = 2; $x < $num_questions - 1; $x++) {
  37. $query = "INSERT INTO `questionnair_question` (`id_questionnair`, `id_question`) VALUES ('".$questionnaire_id."', '".$values[$x]."');";
  38. $result = mysqli_query($connection, $query);
  39. if(!$result) die("Error: ".mysqli_error($connection));
  40. }
  41. // WHY NOT USE header INSTEAD OF window.top.location?
  42. //header('Location: http://emaapp.online/admin_nuevo/questionnaires.php');
  43. //exit();
  44. echo "<script>window.top.location='http://emaapp.online/admin_nuevo/questionnaires.php'</script>";
  45. }