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.


Popular Tutorials Pagination System using Ajax, Jquery and PHPSend Friend Request, Accept or Decline Request using Ajax, Jquery and PHPContact Form with Captcha using Ajax, Jquery and PHPWhy Having a FREE website is bad ChoiceBulk image resize with PHPWeb Browser Notification System using JavaScriptIntroduction to php.ini FileTypes of PHP ErrorsProgram Execution Time Limit in PHPTo-do list using PHP and MySQLiMove File from One Folder to the other using PHPHow to change or convert MySQL to MySQLiHow to Upload Video Files using PHP and MySQLiConvert HTML to MS Word Document using PHPEight Reasons Why You Need A Business WebsiteLocal Web Development Server for Windows - XAMPPEasy Shopping Cart using HTML, CSS & JavaScriptComment System using PHP, Ajax and MySQLiAdd, Edit and Delete Record from Database using jQuery and PHPResponsive Contact Form with Captcha using Jquery and PHPUpdate or Delete Multiple Rows using PHPPassword Strength Checker using JqueryFacebook Style Auto Scroll Pagination using jQuery and PHPDomain Checker using Ajax, Jquery and PHPThe if else & elseif Statements in PHPPreview and upload multiple files using ajax and PHPFancy Multiple File Upload using Ajax, Jquery and PHPPagination System without Database using Ajax and PHPUnderstanding Cookies in JavaScript: Create, Read, Update and DeleteSecured Users Access Level System using PHP and MysqlSecure Forgot Password System Using Ajax, Jquery and PHPChat Script Similar to Facebook using Ajax and PHPMake Dynamic XML sitemap using PHPHow to create an animated old letter using an animated penAuto-load User Details on Mouse-Over using Ajax, Jquery and PHPFacebook style Search People and Products using Ajax, Jquery and PHPAuto-suggestion using Ajax and PHPAuto-suggest System using Ajax, Jquery and PHPUsername Availability Checker using Ajax and PHPSimple PHP Anti-Spam Captcha Contact Form Submission

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...