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

Add, Edit and Delete Record from Database using jQuery and PHP

Posted on 24th October, 2025
PHP ADD RECORD PHP EDIT RECORD PHP DELETE RECORD ADD RECORD TO DATABASE UPDATE DATABASE RECORD
In this tutorial, we assume that you already have a basic knowledge of Ajax/jQuery and PHP in general to get you started

The tutorial teaches you how to build a simple system where you can easily add new records to a database, view and update the added records in the database or delete the records complete from the database using Ajax/jQuery and PHP

The important aspect of this implementation is that the system uses MySqli procedural method for all database related queries and all the above mentioned features are done without refreshing your web page which is the area where Ajax/jQuery comes in.

The Ajax/jQuery helps to enhance user experience at the client side while the PHP handles the server side requests and interacts directly with the database as requests are received from the client side.

What is a database?
A database often abbreviated as DB is basically a collection of information organized to provide efficient retrieval or organized in such a way that a computer program can quickly select desired pieces of data at any given time.

Why do you need a database?
You can query data in a database (ask the database questions).
You can look up data from a database relatively rapidly.
You can relate data from two different tables together using JOINs.
You can create meaningful reports from data in a database.
Your data has a built-in structure to it.
Information of a given type is always stored only once.
Databases are ACID.
Databases are fault-tolerant.
Databases can handle very large data sets.
Databases are concurrent; multiple users can use them at the same time without corrupting the data.
Databases scale well.
In short, you benefit from a wide range of well-known, proven technologies developed over many years by a wide variety of very smart people.

DATABASE CONNECTION SCRIPT

<?php
/**************************************************************************************************
* This script is brought to you by Vasplus Programming Blog
* Website: www.vasplus.info
* Email: info@vasplus.info
***************************************************************************************************/
// Secure this page
if(!defined('VASPLUS_APP')) { die('Access Denied'); exit; } else {}; 

/**
 * These are the database connection details
 */  
 
define("HOST", 'localhost');     // The host you want to connect to.
define("USER", 'root');    		// The database username. 
define("PASSWORD", '');    		// The database password. 
define("DATABASE", 'demo');    	// The database name.

$mysqli = mysqli_connect(HOST, USER, PASSWORD) or die("Unable to connect to MySQL Server: ". mysqli_connect_error());
mysqli_select_db($mysqli, DATABASE) or die("Unable to connect to Database: ". DATABASE);
?>


ADD RECORD SCRIPT

<?php
// Add Records
function add_record($fullname, $email)
{
	global $mysqli;
	
	if( $fullname == "" || $email == "" )
	{
		return 'Sorry, there was a general system error, please refresh this page and try again.';
	}
	else
	{
		if(mysqli_query($mysqli,"insert into `usersRecords` values(0, '".mysqli_real_escape_string($mysqli,$fullname)."', '".mysqli_real_escape_string($mysqli,$email)."')"))
		{
			return 'completed';
		}
		else
		{
			return 'Sorry, the operation could not be completed at the moment, please try again.';
		}
	}
}
?>


UPDATE RECORD SCRIPT

<?php
// Update or Edit Records
function edit_record($fullname, $email, $id)
{
	global $mysqli;
	
	if( $fullname == "" || $email == "" || $id == "" )
	{
		return 'Sorry, there was a general system error, please refresh this page and try again.';
	}
	else
	{
		$check_records = mysqli_query($mysqli,"select * from `usersRecords` where `id` = '".mysqli_real_escape_string($mysqli,$id)."' limit 1");
		
		if(mysqli_num_rows($check_records) > 0)
		{
			if(mysqli_query($mysqli,"update `usersRecords` set `fullname` = '".mysqli_real_escape_string($mysqli,$fullname)."', `email` = '".mysqli_real_escape_string($mysqli,$email)."' where `id` = '".mysqli_real_escape_string($mysqli,$id)."'"))
			{
				return 'completed';
			}
			else
			{
				return 'Sorry, the operation could not be completed at the moment, please try again.';
			}
		}
		else
		{
			echo 'Sorry, the record you were trying to update could not be found in the system at the moment.';
		}
	}
}
?>


DELETE RECORD SCRIPT

<?php
// Delete Records
function delete_record($id)
{
	global $mysqli;
	
	if( $id == "" )
	{
		return 'Sorry, there was a general system error, please refresh this page and try again.';
	}
	else
	{
		if(mysqli_query($mysqli,"delete from `usersRecords` where `id` = '".mysqli_real_escape_string($mysqli,$id)."' limit 1"))
		{
			return 'completed';
		}
		else
		{
			return 'Sorry, the operation could not be completed at the moment, please try again.';
		}
	}
}
?>


LOAD DATA OR RECORDS FROM THE DATABASE SCRIPT

<?php
// Load all the records from the DB
function load_records()
{
	global $mysqli;
	
	$check_records = mysqli_query($mysqli,"select * from `usersRecords` order by `id` desc limit 10");
	
	if(mysqli_num_rows($check_records) > 0)
	{
		?>
        <div style="border:1px solid #E5E5E5; width:100%;">
        
        <div style=" float:left; border-right:1px solid #E5E5E5; width:200px; padding:10px;">Fullname</div>
        <div style=" float:left; border-right:1px solid #E5E5E5; width:220px; padding:10px;">Email Address</div>
        <div style=" float:left; width:100px; padding:10px; text-align:center;">Action</div>
        
        <div style="clear:both;"></div>
        </div>
        
        
        <?php
		while($get_record = mysqli_fetch_array($check_records))
		{
			ob_start();
			
			$id = trim(strip_tags($get_record["id"]));
			$fullname = trim(strip_tags($get_record["fullname"]));
			$email = trim(strip_tags($get_record["email"]));
			
			?>
            
            
            
            <!-- DITABLE RECORDS AREA -->
            <div style="border:1px solid #E5E5E5;border-top:0px solid #E5E5E5; width:100%; display:none;" id="record_a_<?php echo $id; ?>">
            
            <div style=" float:left; border-right:1px solid #E5E5E5; width:206px; padding:7px;">
            <input type="text" id="fullname<?php echo $id; ?>" value="<?php echo $fullname; ?>" />
            </div>
            
            <div style=" float:left; border-right:1px solid #E5E5E5; width:226px; padding:7px;">
            <input type="text" id="email<?php echo $id; ?>" value="<?php echo $email; ?>" />
            </div>
            
            
            <div style=" float:left; width:120px; padding:6px;">
            <input type="button" value="Save" onclick="vpb_record_system('edit', '<?php echo $id; ?>');" />
            <input type="button" value="Cancel" onclick="vpb_make_editable_record('<?php echo $id; ?>');" />
            </div>
            
            <div style="clear:both;"></div>
            </div>
            
            
            
            
            
            
            <!-- DEFAULT RECORDS AREA -->
            <div style="border:1px solid #E5E5E5;border-top:0px solid #E5E5E5; width:100%;" id="record_b_<?php echo $id; ?>">
            
            <div style=" float:left; border-right:1px solid #E5E5E5; width:200px; padding:10px;">
            <span id="fn_<?php echo $id; ?>"><?php echo $fullname; ?></span>
            </div>
            
            <div style=" float:left; border-right:1px solid #E5E5E5; width:220px; padding:10px;">
            <span id="em_<?php echo $id; ?>"><?php echo $email; ?></span>
            </div>
            
            <div style=" float:left; width:120px; padding:6px;">
            <input type="button" value="Edit" onclick="vpb_make_editable_record('<?php echo $id; ?>');" />
            <input type="button" value="Delete" onclick="vpb_record_system('delete', '<?php echo $id; ?>');" />
            </div>
            
            <div style="clear:both;"></div>
            </div>
            
            
            
            
            <?php
			
			ob_end_flush();
		}
	}
	else
	{
		return 'There is no record in the system at the moment.';
	}
}
?>

This is a very simple to understand application as all you need to do are well explained in the README.txt file that comes with the script.

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