exec(" CREATE TABLE IF NOT EXISTS tasks ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, description TEXT, status ENUM('pending', 'completed') DEFAULT 'pending' NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) "); // --- HANDLE POST REQUESTS --- if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (session_status() == PHP_SESSION_NONE) { session_start(); } $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) VALUES (?, ?)"); $stmt->execute([$title, $description]); $_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); if ($task_id) { $stmt = $pdo->prepare("UPDATE tasks SET status = 'completed' WHERE id = ?"); $stmt->execute([$task_id]); $_SESSION['message'] = 'Task marked as completed!'; $_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 = ?"); $stmt->execute([$task_id]); $_SESSION['message'] = 'Task deleted successfully!'; $_SESSION['message_type'] = 'success'; } } // Redirect to self to prevent form resubmission header("Location: " . $_SERVER['PHP_SELF']); exit; } // Check for session message if (session_status() == PHP_SESSION_NONE) { session_start(); } if (isset($_SESSION['message'])) { $message = $_SESSION['message']; $message_type = $_SESSION['message_type']; unset($_SESSION['message']); unset($_SESSION['message_type']); } // --- FETCH TASKS FOR DISPLAY --- $stmt = $pdo->query("SELECT * FROM tasks ORDER BY status ASC, created_at DESC"); $tasks = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error_message = "Database error: " . $e->getMessage(); $tasks = []; $message = $error_message; $message_type = 'danger'; } ?> MyTaskManager

Add a New Task

Your Tasks

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