prepare("SELECT * FROM users WHERE username = :username LIMIT 1"); $stmt->bindValue(':username', $username, PDO::PARAM_STR); $stmt->execute(); $user = $stmt->fetch(PDO::FETCH_ASSOC); if ($user && password_verify($password, $user['password'])) { // Successful login $_SESSION['admin_logged_in'] = true; header('Location: admin.php'); exit; } else { $error = "Invalid credentials. Please try again."; } } // ------------------------------ // Handle Admin Logout // ------------------------------ if (isset($_GET['action']) && $_GET['action'] === 'logout') { session_destroy(); header('Location: admin.php'); exit; } // ------------------------------ // Handle Creating a New Poll // ------------------------------ if (isset($_POST['create_poll']) && isAdminLoggedIn()) { $questionText = trim($_POST['question_text'] ?? ''); if (!empty($questionText)) { $stmt = $db->prepare("INSERT INTO poll_questions (question_text) VALUES (:question_text)"); $stmt->bindValue(':question_text', $questionText, PDO::PARAM_STR); $stmt->execute(); $successMsg = "Poll question created successfully!"; } else { $errorMsg = "Please enter a question text."; } } // ------------------------------ // Handle Adding Options to an Existing Poll // ------------------------------ if (isset($_POST['add_option']) && isAdminLoggedIn()) { $questionId = (int)($_POST['poll_id'] ?? 0); $optionText = trim($_POST['option_text'] ?? ''); if ($questionId > 0 && !empty($optionText)) { // Check if poll question exists $stmt = $db->prepare("SELECT id FROM poll_questions WHERE id = :id"); $stmt->bindValue(':id', $questionId, PDO::PARAM_INT); $stmt->execute(); if ($stmt->fetchColumn()) { // Insert the new option $insertOption = $db->prepare(" INSERT INTO poll_options (question_id, option_text) VALUES (:question_id, :option_text) "); $insertOption->bindValue(':question_id', $questionId, PDO::PARAM_INT); $insertOption->bindValue(':option_text', $optionText, PDO::PARAM_STR); $insertOption->execute(); // Also initialize poll_results with a 0 vote count for the new option $optionId = $db->lastInsertId(); $insertResult = $db->prepare(" INSERT INTO poll_results (question_id, option_id, vote_count) VALUES (:question_id, :option_id, 0) "); $insertResult->bindValue(':question_id', $questionId, PDO::PARAM_INT); $insertResult->bindValue(':option_id', $optionId, PDO::PARAM_INT); $insertResult->execute(); $successMsg = "Option added successfully!"; } else { $errorMsg = "Poll question does not exist."; } } else { $errorMsg = "Please select a poll and enter an option text."; } } // ------------------------------ // Handle Editing an Existing Poll // ------------------------------ if (isset($_POST['edit_poll']) && isAdminLoggedIn()) { $pollId = (int)($_POST['poll_id'] ?? 0); $newQuestionText = trim($_POST['edit_question_text'] ?? ''); if ($pollId > 0 && !empty($newQuestionText)) { // Check if poll question exists $checkStmt = $db->prepare("SELECT id FROM poll_questions WHERE id = :id"); $checkStmt->bindValue(':id', $pollId, PDO::PARAM_INT); $checkStmt->execute(); if ($checkStmt->fetchColumn()) { // Update the poll question $updateStmt = $db->prepare(" UPDATE poll_questions SET question_text = :question_text WHERE id = :id "); $updateStmt->bindValue(':question_text', $newQuestionText, PDO::PARAM_STR); $updateStmt->bindValue(':id', $pollId, PDO::PARAM_INT); $updateStmt->execute(); $successMsg = "Poll question updated successfully!"; } else { $errorMsg = "Poll question does not exist."; } } else { $errorMsg = "Invalid poll ID or question text."; } } // ------------------------------ // Handle Deleting an Existing Poll // ------------------------------ if (isset($_POST['delete_poll']) && isAdminLoggedIn()) { $pollId = (int)($_POST['poll_id'] ?? 0); if ($pollId > 0) { // Check if poll question exists $checkStmt = $db->prepare("SELECT id FROM poll_questions WHERE id = :id"); $checkStmt->bindValue(':id', $pollId, PDO::PARAM_INT); $checkStmt->execute(); if ($checkStmt->fetchColumn()) { // Delete poll_results $deleteResults = $db->prepare("DELETE FROM poll_results WHERE question_id = :id"); $deleteResults->bindValue(':id', $pollId, PDO::PARAM_INT); $deleteResults->execute(); // Delete poll_options $deleteOptions = $db->prepare("DELETE FROM poll_options WHERE question_id = :id"); $deleteOptions->bindValue(':id', $pollId, PDO::PARAM_INT); $deleteOptions->execute(); // Finally, delete the poll question $deletePoll = $db->prepare("DELETE FROM poll_questions WHERE id = :id"); $deletePoll->bindValue(':id', $pollId, PDO::PARAM_INT); $deletePoll->execute(); $successMsg = "Poll deleted successfully!"; } else { $errorMsg = "Poll question does not exist."; } } else { $errorMsg = "Invalid poll ID."; } } // ------------------------------ // Fetch All Polls for Display // ------------------------------ $polls = []; if (isAdminLoggedIn()) { $pollsQuery = $db->query("SELECT id, question_text, created_at FROM poll_questions ORDER BY id DESC"); $polls = $pollsQuery->fetchAll(PDO::FETCH_ASSOC); } ?>
No polls available. Create a new poll first.
No polls to display.