Creating a Simple To Do List App in PHP

Explore a detailed guide alongside code snippets illustrating the creation of a basic to-do list application using PHP and MySQL, complete with free source code. The application boasts a polished design, thanks to Bootstrap integration, giving it a professional appearance.

Highlighted below are the functionalities encompassed by this uncomplicated source code:

  1. Task Addition: Seamlessly incorporate new tasks to your to-do list.
  2. Status Update: Swiftly transition task statuses to "Done" for efficient tracking.
  3. Task Deletion: Effortlessly remove tasks that have been completed or are no longer relevant.

Moreover, this straightforward foundation can readily support the integration of additional functionalities, such as task editing, due dates, reminders, and more. This versatile framework serves as an excellent starting point for building a more robust task management system tailored to your needs.

So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that has been used for the layout https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_task. After that, click Import then locate the database file inside the folder of the application then click ok.

tut1

You can also add the database table programmatically. To do this, copy and paste the code below into the PHPMyAdmin SQL Tab in your newly created database.

  1. CREATE TABLE `task` (
  2. `task_id` int(11) NOT NULL,
  3. `task` varchar(150) NOT NULL,
  4. `status` varchar(150) NOT NULL
  5.  
  6. INSERT INTO `task` (`task_id`, `task`, `status`) VALUES
  7. (1, 'Check Errors', 'Done'),
  8. (4, 'Remove Bugs', ''),
  9. (5, 'Need Improvements', '');
  10.  
  11. ALTER TABLE `task`
  12. ADD PRIMARY KEY (`task_id`);
  13.  
  14. ALTER TABLE `task`

Creating the Database Connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = new mysqli("localhost", "root", "", "db_task");
  3.  
  4. if(!$conn){
  5. die("Error: Cannot connect to the database");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and paste it into you text editor, then save it as shown below.

index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple To Do List App</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <center>
  20. <form method="POST" class="form-inline" action="add_query.php">
  21. <input type="text" class="form-control" name="task" required/>
  22. <button class="btn btn-primary form-control" name="add">Add Task</button>
  23. </form>
  24. </center>
  25. </div>
  26. <br /><br /><br />
  27. <table class="table">
  28. <thead>
  29. <tr>
  30. <th>#</th>
  31. <th>Task</th>
  32. <th>Status</th>
  33. <th>Action</th>
  34. </tr>
  35. </thead>
  36. <tbody>
  37. <?php
  38. require 'conn.php';
  39. $query = $conn->query("SELECT * FROM `task` ORDER BY `task_id` ASC");
  40. $count = 1;
  41. while($fetch = $query->fetch_array()){
  42. ?>
  43. <tr>
  44. <td><?php echo $count++?></td>
  45. <td><?php echo $fetch['task']?></td>
  46. <td><?php echo $fetch['status']?></td>
  47. <td colspan="2">
  48. <center>
  49. <?php
  50. if($fetch['status'] != "Done"){
  51. echo
  52. '<a href="update_task.php?task_id='.$fetch['task_id'].'" class="btn btn-success"><span class="glyphicon glyphicon-check"></span></a> |';
  53. }
  54. ?>
  55. <a href="delete_query.php?task_id=<?php echo $fetch['task_id']?>" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span></a>
  56. </center>
  57. </td>
  58. </tr>
  59. <?php
  60. }
  61. ?>
  62. </tbody>
  63. </table>
  64. </div>
  65. </body>
  66. </html>

Creating the functions

This code contains a specific function for the application. This is where the function works where you can manipulate the data. This code consists of several methods that can ADD and DELETE the data in the database. To do that write these codes inside the text editor and save them as corresponds file names.

add_query.php
  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_POST['add'])){
  4. if($_POST['task'] != ""){
  5. $task = $_POST['task'];
  6.  
  7. $conn->query("INSERT INTO `task` VALUES('', '$task', '')");
  8. header('location:index.php');
  9. }
  10. }
  11. ?>
update_task.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if($_GET['task_id'] != ""){
  5. $task_id = $_GET['task_id'];
  6.  
  7. $conn->query("UPDATE `task` SET `status` = 'Done' WHERE `task_id` = $task_id") or die(mysqli_errno($conn));
  8. header('location: index.php');
  9. }
  10. ?>
delete_query.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if($_GET['task_id']){
  5. $task_id = $_GET['task_id'];
  6.  
  7. $conn->query("DELETE FROM `task` WHERE `task_id` = $task_id") or die(mysqli_errno($conn));
  8. header("location: index.php");
  9. }
  10. ?>

Demo

There you have it we successfully created a Simple To-Do List App using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!

Comments

Submitted bytushar 564654 (not verified)on Tue, 06/15/2021 - 20:32

not working bro.
Submitted byLuis Hernández (not verified)on Tue, 07/20/2021 - 23:03

When i tried add task, not happen anything, the solution is add simple coma to var $task inside the query
Submitted byjim234234 (not verified)on Mon, 01/03/2022 - 10:10

doesn't work! i can update and delete items but when i add stuff nothing happens
Submitted byAlbert john (not verified)on Wed, 02/23/2022 - 02:47

I read your excellent post. After reading this post, i really appreciate your effort and my request is to please share us more post in future. Keep it up. It is one of the best information which you are providing. I love your blog because it totally full of informative posts. You are nice one and thanks for sharing. The App Development Dubai provide good service of app development you visit here site for more info.
Submitted byMick John (not verified)on Tue, 04/19/2022 - 01:20

Two full thumbs up for this magnificent article of yours. I've really enjoyed reading this article today and I think this might be one of the best article that I've read yet. Please, keep this work going on in the same quality and thanks for sharing. If you want Remote Job so visit here site for more info from Smart Job.
Submitted byshortybsd (not verified)on Fri, 10/07/2022 - 19:48

For people with issues, tons of things can be changed but I will only show you the most important. Creating the database all you need is following, no need for sample data.
  1. CREATE TABLE `task` (
  2. `task_id` INT AUTO_INCREMENT,
  3. `task` varchar(150) NOT NULL,
  4. `status` varchar(150) NOT NULL,
  5. PRIMARY KEY(task_id));
No need for all the alters. For people with issues why tasks will not add, it is because you cannot use blank data for the id if it is auto incremented. To fix this specifically call on 2 fields to update as the id field is automatic In add_query.php change line 7 to the following and everything will work perfect. $conn->query("INSERT INTO `task` (task, status) VALUES('$task', '')"); Last but not least, limit the text box for 1500 characters so users don't type too much. Line 19 change it to the following <input type="text" class="form-control" maxlength="150" name="task" required/>
Submitted bySecretWeb (not verified)on Tue, 11/29/2022 - 15:49

Awesome! it's working, thank you for sharing simple project it help me a lot to learn new things.
Submitted byBud Kaye (not verified)on Fri, 09/08/2023 - 05:01

Howdy! If we use an apostrophe when adding a TASK, it doesn't work! Unless we escape the apostrophe with a backslash before it... Any help for this?? Thanks!!

Add new comment