Add, Edit and Delete Record from Database using jQuery and PHP
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.
Screen Shots
Click on a photo below to scroll through the screen shots of the application!
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.
