connect_error) { die("Connection failed: " . $db->connect_error); } $survey_id = $_GET['id'] ?? null; $is_edit = isset($_GET['action']) && $_GET['action'] === 'edit_survey'; if ($is_edit && $survey_id) { // Fetch survey details $survey = $db->query("SELECT * FROM surveys WHERE id = $survey_id")->fetch_assoc(); // Fetch pages and choices $pages_query = $db->query("SELECT p.*, c.id as choice_id, c.choice_text, c.redirect_url FROM pages p LEFT JOIN choices c ON p.id = c.page_id WHERE p.survey_id = $survey_id ORDER BY p.page_number, c.id"); $pages_data = []; while ($row = $pages_query->fetch_assoc()) { $page_number = $row['page_number']; if (!isset($pages_data[$page_number])) { $pages_data[$page_number] = [ 'question_text' => $row['question_text'], 'choices' => [] ]; } if ($row['choice_id']) { $pages_data[$page_number]['choices'][] = [ 'choice_text' => $row['choice_text'], 'redirect_url' => $row['redirect_url'] ]; } } } else { // Default values for new survey $survey = [ 'title' => 'Dummy Survey Title', 'welcome_content' => 'Welcome to our survey! We appreciate your participation.', 'unique_id' => '' ]; $pages_data = [ 1 => ['question_text' => 'Welcome to the survey', 'choices' => []], 2 => ['question_text' => 'What is your favorite color?', 'choices' => [['choice_text' => 'Red'], ['choice_text' => 'Blue', 'redirect_url' => '']]], 3 => ['question_text' => 'How often do you exercise?', 'choices' => [['choice_text' => 'Daily'], ['choice_text' => 'Weekly', 'redirect_url' => '']]], 4 => ['question_text' => 'Do you prefer coffee or tea?', 'choices' => [['choice_text' => 'Coffee'], ['choice_text' => 'Tea', 'redirect_url' => '']]], 5 => ['question_text' => 'Thank you for completing the survey!', 'choices' => [['choice_text' => 'Finish', 'redirect_url' => '']]] ]; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = $db->real_escape_string($_POST['title']); $welcome_content = $db->real_escape_string($_POST['welcome_content']); $unique_id = $is_edit ? $survey['unique_id'] : $db->real_escape_string($_POST['unique_id']); if ($is_edit) { $db->query("UPDATE surveys SET title = '$title', welcome_content = '$welcome_content' WHERE id = $survey_id"); } else { $db->query("INSERT INTO surveys (title, welcome_content, unique_id) VALUES ('$title', '$welcome_content', '$unique_id')"); $survey_id = $db->insert_id; } for ($i = 2; $i <= 5; $i++) { $question_text = $db->real_escape_string($_POST["question_$i"]); if ($is_edit) { $db->query("UPDATE pages SET question_text = '$question_text' WHERE survey_id = $survey_id AND page_number = $i"); $page_id = $db->query("SELECT id FROM pages WHERE survey_id = $survey_id AND page_number = $i")->fetch_assoc()['id']; } else { $db->query("INSERT INTO pages (survey_id, page_number, question_text) VALUES ($survey_id, $i, '$question_text')"); $page_id = $db->insert_id; } if ($i < 5) { $choice1 = $db->real_escape_string($_POST["choice1_$i"]); $choice2 = $db->real_escape_string($_POST["choice2_$i"]); $redirect2 = $db->real_escape_string($_POST["redirect2_$i"]); if ($is_edit) { $db->query("UPDATE choices SET choice_text = '$choice1', redirect_url = NULL WHERE page_id = $page_id AND id = (SELECT id FROM (SELECT id FROM choices WHERE page_id = $page_id LIMIT 1) AS subquery)"); $db->query("UPDATE choices SET choice_text = '$choice2', redirect_url = " . ($redirect2 ? "'$redirect2'" : "NULL") . " WHERE page_id = $page_id AND id = (SELECT id FROM (SELECT id FROM choices WHERE page_id = $page_id LIMIT 1,1) AS subquery)"); } else { $db->query("INSERT INTO choices (page_id, choice_text, redirect_url) VALUES ($page_id, '$choice1', NULL)"); $db->query("INSERT INTO choices (page_id, choice_text, redirect_url) VALUES ($page_id, '$choice2', " . ($redirect2 ? "'$redirect2'" : "NULL") . ")"); } } else { $finish_redirect = $db->real_escape_string($_POST['finish_redirect']); if ($is_edit) { $db->query("UPDATE choices SET redirect_url = " . ($finish_redirect ? "'$finish_redirect'" : "NULL") . " WHERE page_id = $page_id LIMIT 1"); } else { $db->query("INSERT INTO choices (page_id, choice_text, redirect_url) VALUES ($page_id, 'Finish', " . ($finish_redirect ? "'$finish_redirect'" : "NULL") . ")"); } } } $_SESSION['message'] = $is_edit ? "Survey updated successfully!" : "Survey created successfully!"; $_SESSION['message_type'] = "success"; header("Location: index.php"); exit; } ?>