Loading.......
Loading...

To-do list using PHP and MySQLi

Posted on 23rd October, 2021
TO-DO LIST PHP TO-DO LIST MYSQL TO-DO LIST
This tutorial teaches you how to implement or design a To-do list that can keep proper record of your activities or tasks using PHP and MySQLi

The tutorial is beneficial to as many who likes writing down their tasks or activities in a jotter and mark each of those tasks completed when they are done with them.

It is simple todo-list application that takes record of tasks submitted by end users in a form and saves them with a Pending status in a MySQL database.

The tasks are also retrieved from the MySQL database and displayed on a web page with an update and delete button beside each task.

When the update button is clicked, it goes to change the pending status of the task to Completed and when the delete button also is clicked, the task gets deleted from the database.

DATABASE CONNECTION SCRIPT

<?php
// Fill in your database hostname, username, password and database name below

$mysqli = new mysqli("localhost", "root", "", "to_do_list");
if(!$mysqli)
{
	die("Error: Cannot connect to the database");
}
?>


ADD TASK SCRIPT

<?php
// Check to be sure that the only request we can process is POST REQUEST
if(isset($_SERVER["REQUEST_METHOD"]) && strip_tags($_SERVER["REQUEST_METHOD"]) == strip_tags("POST"))
{
	if(isset($_POST['add']))
	{
		if( isset($_POST['task']) && !empty($_POST['task']) ) // Be sure the task brought here is not an empty field
		{
			// Variable declaration
			$task = trim(strip_tags(htmlspecialchars($_POST['task'])));
			
			// Save the task into the database
			mysqli_query($mysqli, "insert into `task` values(0, '".mysqli_real_escape_string($mysqli, $task)."', '".mysqli_real_escape_string($mysqli, 'Pending')."')") or die(mysqli_errno());
			
			mysqli_query($mysqli, "delete from `task` order by `task_id` asc limit 1");
			
			echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
		}
		else
		{
			echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
		}
	}
	else
	{
		echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
		//die('Sorry, no proper data was passed');
	}
}
else 
{
	echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
	// Deny access if the request brought to this page is not a POST REQUEST
	//die('Access Denied');
}
?>


UPDATE TASK SCRIPT

<?php
// Check to be sure that the only request we can process is POST REQUEST
if(isset($_SERVER["REQUEST_METHOD"]) && strip_tags($_SERVER["REQUEST_METHOD"]) == strip_tags("GET"))
{
	
	if(isset($_GET['task_id']) && !empty($_GET['task_id'])) // Be sure the task ID brought here is not an empty field
	{
		// Variable declaration
		$task_id = trim(strip_tags($_GET['task_id']));
		
		// Update the database to set the task to Completed
		mysqli_query($mysqli, "update `task` set `status` = '".mysqli_real_escape_string($mysqli, 'Completed')."' where `task_id` = '".mysqli_real_escape_string($mysqli, $task_id)."' limit 1") or die(mysqli_errno());
		
		echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
	}
	else
	{
		echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
		//die('Sorry, no proper data was passed');
	}
}
else 
{
	echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
	// Deny access if the request brought to this page is not a GET REQUEST
	//die('Access Denied');
}
?>


DELETE TASK SCRIPT

<?php
// Check to be sure that the only request we can process is POST REQUEST
if(isset($_SERVER["REQUEST_METHOD"]) && strip_tags($_SERVER["REQUEST_METHOD"]) == strip_tags("GET"))
{
	if(isset($_GET['task_id']) && !empty($_GET['task_id'])) // Be sure the task ID brought here is not an empty field
	{
		// Variable declaration
		$task_id = trim(strip_tags($_GET['task_id']));
		
		// Delete the task from the database
		mysqli_query($mysqli, "delete from `task` where `task_id` = '".mysqli_real_escape_string($mysqli, $task_id)."' limit 1") or die(mysqli_errno());
		
		echo "<script>window.location='index.php'</script>"; // Redirect back to index.php page
	}	
	else
	{
		echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
		//die('Sorry, no proper data was passed');
	}
}
else 
{
	echo "<script>window.location='index.php'</script>";  // Redirect back to index.php page
	// Deny access if the request brought to this page is not a GET REQUEST
	//die('Access Denied');
}
?>

The script is very easy to understand and customize with programming code comments to ease usability.

To see the system in action, please click on the Live Demo button below and click on the Download button to download the script if you like it.

Thank You!
The Vasplus Team.
Post Comment
Press Enter to send
No comment yet.

Submit your Job or Project Today!

We can help you turn your idea into reality, take over your existing project, or extend your current development team.

Submit your idea job or project below and we will follow up with you shortly.

OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Please Wait....
Please Wait...