Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
CREATE DATABASE todo_list; USE todo_list; CREATE TABLE tasks ( id INT AUTO_INCREMENT PRIMARY KEY, task VARCHAR(255) NOT NULL, status TINYINT(1) DEFAULT 0 );

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php include 'db.php'; // Add Task if (isset($_POST['add'])) { $task = $_POST['task']; $sql = "INSERT INTO tasks (task) VALUES ('$task')"; $conn->query($sql); } // Mark as Completed if (isset($_GET['complete'])) { $id = $_GET['complete']; $conn->query("UPDATE tasks SET status=1 WHERE id=$id"); } // Delete Task if (isset($_GET['delete'])) { $id = $_GET['delete']; $conn->query("DELETE FROM tasks WHERE id=$id"); } // Fetch Tasks $result = $conn->query("SELECT * FROM tasks ORDER BY id DESC"); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>To-Do List</title> <style> body { font-family: Arial, sans-serif; margin: 50px; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 10px; text-align: left; } th { background-color: #f4f4f4; } .completed { text-decoration: line-through; color: green; } </style> </head> <body> <h2>To-Do List</h2> <form method="POST"> <input type="text" name="task" required> <button type="submit" name="add">Add Task</button> </form> <table> <tr> <th>Task</th> <th>Actions</th> </tr> <?php while ($row = $result->fetch_assoc()): ?> <tr> <td class="<?= $row['status'] ? 'completed' : '' ?>"> <?= $row['task'] ?> </td> <td> <?php if (!$row['status']): ?> <a href="?complete=<?= $row['id'] ?>">✔ Complete</a> <?php endif; ?> <a href="?delete=<?= $row['id'] ?>" onclick="return confirm('Delete this task?')">🗑 Delete</a> </td> </tr> <?php endwhile; ?> </table> </body> </html>
Copy Clear