Home
Home
AllPHPTricks Logo
Insert, View, Edit and Delete Record from Database Using PHP and MySQLi
Demo Download
In this tutorial, I will explain how to insert, view, edit and delete record from database using PHP and
Mysqli, basically this tutorial is a second part of Simple User Registration & Login Script in PHP and
MySQLi, in this first part I explained how to create simple user registration and login using PHP and
MySQLi, if you do not know how to create user registration and user login form so kindly first check this
tutorial Simple User Registration & Login Script in PHP and MySQLi, after that now come back to this
tutorial.
I have tested this tutorial code from PHP 5.6 to PHP 8.2 versions, it is working smoothly on all PHP
versions. However, I would recommend to use the latest PHP version.
I would suggest you to check PHP CRUD Operations Using PDO Prepared Statements, as this is
compatible with PHP 7 and PHP 8 both versions.
Steps to Creating an Insert, View, Edit and Delete Record from Database Using PHP and MySQLi
Now I am assuming that you have already created user registration and login forms which I created in
Simple User Registration & Login Script in PHP and MySQLi now I will create another table to keeping
records in it, update dashboard.php file and add four more new pages which are insert.php, view.php,
edit.php, and delete.php, follow the following steps:
);
<?php
include("auth.php");
require('db.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div class="form">
<p>Welcome to Dashboard.</p>
<p><a href="index.php">Home</a><p>
<p><a href="logout.php">Logout</a></p>
</div>
</body>
</html>
Create a page with name insert.php and paste the below code in it.
<?php
include("auth.php");
require('db.php');
$status = "";
$name =$_REQUEST['name'];
$age = $_REQUEST['age'];
$submittedby = $_SESSION["username"];
(`trn_date`,`name`,`age`,`submittedby`)values
('$trn_date','$name','$age','$submittedby')";
mysqli_query($con,$ins_query)
or die(mysqli_error($con));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div class="form">
<p><a href="dashboard.php">Dashboard</a>
| <a href="logout.php">Logout</a></p>
<div>
</form>
</div>
</div>
</body>
</html>
insert-new-record
Create a page with name view.php and paste the below code in it.
<?php
include("auth.php");
require('db.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>View Records</title>
</head>
<body>
<div class="form">
<p><a href="index.php">Home</a>
| <a href="logout.php">Logout</a></p>
<h2>View Records</h2>
<thead>
<tr>
<th><strong>S.No</strong></th>
<th><strong>Name</strong></th>
<th><strong>Age</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
$result = mysqli_query($con,$sel_query);
<td align="center">
</td>
<td align="center">
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
view-record
Create a page with name edit.php and paste the below code in it.
<?php
include("auth.php");
require('db.php');
$id=$_REQUEST['id'];
$row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Update Record</title>
</head>
<body>
<div class="form">
<p><a href="dashboard.php">Dashboard</a>
| <a href="logout.php">Logout</a></p>
<h1>Update Record</h1>
<?php
$status = "";
$id=$_REQUEST['id'];
$name =$_REQUEST['name'];
$age =$_REQUEST['age'];
$submittedby = $_SESSION["username"];
name='".$name."', age='".$age."',
}else {
?>
<div>
</form>
<?php } ?>
</div>
</div>
</body>
</html>
update-record
Create a page with name delete.php and paste the below code in it.
<?php
require('db.php');
$id=$_REQUEST['id'];
header("Location: view.php");
exit();
?>
Demo Download
Click here to learn how to implement forgot password recovery (reset) using PHP and MySQL.
This tutorial is only to explain the basic concept of PHP CRUD operations, you will need to implement
more security when working with live or production environment.
If you found this tutorial helpful so share it with your friends, developer groups and leave your
comment.
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development
tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.
Javed Ur Rehman
I build ecommerce websites, web applications, web portals, and online stores to help businesses to
grow.
[email protected]
Join Social Communities
Testimonials
“It was a pleasure working with Javed who created an amazing online shopping cart for us in a record
time. He is very cooperative, polite, knowledgeable, and finds solutions to deliver everything that we
need.”
“He under promises and over delivers. He is very creative and creates very elegant, efficient and
functional solutions.”
“He is going to be our go-to person for any future work that we have and we would highly highly
recommend him in his area of expertise.”
Recent Posts
Insert, View, Edit and Delete Record from Database Using PHP and MySQLi
Go to Top