exec("ALTER TABLE tasks ADD COLUMN user_id INT NULL;"); } catch (PDOException $e) { // Ignore error if the column already exists if (strpos($e->getMessage(), 'Duplicate column name') === false) { throw $e; // Re-throw if it's a different error } } // --- HANDLE POST REQUESTS --- if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; if ($action === 'add_task') { $title = trim($_POST['title'] ?? ''); $description = trim($_POST['description'] ?? ''); if (!empty($title)) { $stmt = $pdo->prepare("INSERT INTO tasks (title, description, user_id) VALUES (?, ?, ?)"); $stmt->execute([$title, $description, $user_id]); $_SESSION['message'] = 'Task added successfully!'; $_SESSION['message_type'] = 'success'; } else { $_SESSION['message'] = 'Task title cannot be empty.'; $_SESSION['message_type'] = 'danger'; } } elseif ($action === 'update_status') { $task_id = filter_var($_POST['task_id'] ?? 0, FILTER_VALIDATE_INT); $status = $_POST['status'] ?? 'pending'; // Get current status to toggle if ($task_id) { // Correctly toggle between pending and completed $new_status = ($status === 'completed') ? 'pending' : 'completed'; $stmt = $pdo->prepare("UPDATE tasks SET status = ? WHERE id = ? AND user_id = ?"); $stmt->execute([$new_status, $task_id, $user_id]); $_SESSION['message'] = 'Task status updated!'; $_SESSION['message_type'] = 'success'; } } elseif ($action === 'delete_task') { $task_id = filter_var($_POST['task_id'] ?? 0, FILTER_VALIDATE_INT); if ($task_id) { $stmt = $pdo->prepare("DELETE FROM tasks WHERE id = ? AND user_id = ?"); $stmt->execute([$task_id, $user_id]); $_SESSION['message'] = 'Task deleted successfully!'; $_SESSION['message_type'] = 'success'; } } // Redirect to self to prevent form resubmission header("Location: " . $_SERVER['PHP_SELF']); exit; } // --- FETCH TASKS FOR DISPLAY --- // Ensure the 'status' column exists before querying, or handle its absence gracefully. // For now, we assume it exists or the ALTER TABLE above would have created it. $stmt = $pdo->prepare("SELECT id, title, description, status, created_at FROM tasks WHERE user_id = ? ORDER BY status ASC, created_at DESC"); $stmt->execute([$user_id]); $tasks = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { // Log the error for debugging purposes error_log("Database error: " . $e->getMessage()); // Provide a user-friendly error message $error_message = "A database error occurred. Please try again later."; $tasks = []; // Ensure tasks is empty on error $_SESSION['message'] = $error_message; $_SESSION['message_type'] = 'danger'; // If the error was specifically about a missing 'status' column, we might want to try fetching without it or prompt for migration. // For this iteration, we'll assume the ALTER TABLE above handles it or the user will address it. } // The header is now included AFTER all the logic, ensuring it's only included if the script runs successfully. require_once __DIR__ . '/includes/header.php'; ?>

Add a New Task

Your Tasks

You have no tasks yet. Add one above to get started!