0% found this document useful (0 votes)
3 views14 pages

Home

This tutorial provides a comprehensive guide on how to insert, view, edit, and delete records in a database using PHP and MySQLi. It includes step-by-step instructions for creating necessary pages and updating the dashboard, along with sample code for each operation. The tutorial is designed for users who have already completed a basic user registration and login script in PHP and MySQLi.

Uploaded by

yohannisdenkeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Home

This tutorial provides a comprehensive guide on how to insert, view, edit, and delete records in a database using PHP and MySQLi. It includes step-by-step instructions for creating necessary pages and updating the dashboard, along with sample code for each operation. The tutorial is designed for users who have already completed a basic user registration and login script in PHP and MySQLi.

Uploaded by

yohannisdenkeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

HomeDemoPortfolioAboutContact Us

AllPHPTricks Logo

Insert, View, Edit and Delete Record from Database Using PHP and MySQLi

By Javed Ur Rehman in MySQL, PHP Last Updated: October 18, 2023

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:

Create Another Table for Records


Update Dashboard File

Create Insert Page

Create View Page

Create Edit/Update Page

Create Delete Page

1. Create Another Table for Records

Execute the below SQL query:

CREATE TABLE IF NOT EXISTS `new_record` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`trn_date` datetime NOT NULL,

`name` varchar(50) NOT NULL,

`age`int(11) NOT NULL,

`submittedby` varchar(50) NOT NULL,

PRIMARY KEY (`id`)

);

2. Update Dashboard Page

Update dashboard.php file and paste the following code in it.

<?php

include("auth.php");

require('db.php');

?>

<!DOCTYPE html>

<html>
<head>

<meta charset="utf-8">

<title>Dashboard - Secured Page</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p>Welcome to Dashboard.</p>

<p><a href="index.php">Home</a><p>

<p><a href="insert.php">Insert New Record</a></p>

<p><a href="view.php">View Records</a><p>

<p><a href="logout.php">Logout</a></p>

</div>

</body>

</html>

3. Create Insert Page

Create a page with name insert.php and paste the below code in it.

<?php

include("auth.php");

require('db.php');

$status = "";

if(isset($_POST['new']) && $_POST['new']==1){

$trn_date = date("Y-m-d H:i:s");

$name =$_REQUEST['name'];
$age = $_REQUEST['age'];

$submittedby = $_SESSION["username"];

$ins_query="insert into new_record

(`trn_date`,`name`,`age`,`submittedby`)values

('$trn_date','$name','$age','$submittedby')";

mysqli_query($con,$ins_query)

or die(mysqli_error($con));

$status = "New Record Inserted Successfully.

</br></br><a href='view.php'>View Inserted Record</a>";

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Insert New Record</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p><a href="dashboard.php">Dashboard</a>

| <a href="view.php">View Records</a>

| <a href="logout.php">Logout</a></p>

<div>

<h1>Insert New Record</h1>


<form name="form" method="post" action="">

<input type="hidden" name="new" value="1" />

<p><input type="text" name="name" placeholder="Enter Name" required /></p>

<p><input type="text" name="age" placeholder="Enter Age" required /></p>

<p><input name="submit" type="submit" value="Submit" /></p>

</form>

<p style="color:#FF0000;"><?php echo $status; ?></p>

</div>

</div>

</body>

</html>

insert-new-record

Readers Also Read: Laravel 10 CRUD Application

4. Create View Page

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>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p><a href="index.php">Home</a>

| <a href="insert.php">Insert New Record</a>

| <a href="logout.php">Logout</a></p>

<h2>View Records</h2>

<table width="100%" border="1" style="border-collapse:collapse;">

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

$sel_query="Select * from new_record ORDER BY id desc;";

$result = mysqli_query($con,$sel_query);

while($row = mysqli_fetch_assoc($result)) { ?>


<tr><td align="center"><?php echo $count; ?></td>

<td align="center"><?php echo $row["name"]; ?></td>

<td align="center"><?php echo $row["age"]; ?></td>

<td align="center">

<a href="edit.php?id=<?php echo $row["id"]; ?>">Edit</a>

</td>

<td align="center">

<a href="delete.php?id=<?php echo $row["id"]; ?>">Delete</a>

</td>

</tr>

<?php $count++; } ?>

</tbody>

</table>

</div>

</body>

</html>

view-record

Readers Also Read: Laravel 10 User Roles and Permissions

5. Create Edit/Update Page

Create a page with name edit.php and paste the below code in it.

<?php

include("auth.php");

require('db.php');
$id=$_REQUEST['id'];

$query = "SELECT * from new_record where id='".$id."'";

$result = mysqli_query($con, $query) or die ( mysqli_error($con));

$row = mysqli_fetch_assoc($result);

?>

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Update Record</title>

<link rel="stylesheet" href="css/style.css" />

</head>

<body>

<div class="form">

<p><a href="dashboard.php">Dashboard</a>

| <a href="insert.php">Insert New Record</a>

| <a href="logout.php">Logout</a></p>

<h1>Update Record</h1>

<?php

$status = "";

if(isset($_POST['new']) && $_POST['new']==1)

$id=$_REQUEST['id'];

$trn_date = date("Y-m-d H:i:s");

$name =$_REQUEST['name'];
$age =$_REQUEST['age'];

$submittedby = $_SESSION["username"];

$update="update new_record set trn_date='".$trn_date."',

name='".$name."', age='".$age."',

submittedby='".$submittedby."' where id='".$id."'";

mysqli_query($con, $update) or die(mysqli_error($con));

$status = "Record Updated Successfully. </br></br>

<a href='view.php'>View Updated Record</a>";

echo '<p style="color:#FF0000;">'.$status.'</p>';

}else {

?>

<div>

<form name="form" method="post" action="">

<input type="hidden" name="new" value="1" />

<input name="id" type="hidden" value="<?php echo $row['id'];?>" />

<p><input type="text" name="name" placeholder="Enter Name"

required value="<?php echo $row['name'];?>" /></p>

<p><input type="text" name="age" placeholder="Enter Age"

required value="<?php echo $row['age'];?>" /></p>

<p><input name="submit" type="submit" value="Update" /></p>

</form>

<?php } ?>

</div>

</div>

</body>
</html>

update-record

6. Create Delete Page

Create a page with name delete.php and paste the below code in it.

<?php

require('db.php');

$id=$_REQUEST['id'];

$query = "DELETE FROM new_record WHERE id=$id";

$result = mysqli_query($con,$query) or die ( mysqli_error($con));

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.

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks


Article By Javed Ur Rehman

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.

Readers also read

Laravel 10 Livewire User Registration and Login

Laravel 10 Livewire User Registration and Login

Laravel 10 User Roles and Permissions

Simple Laravel 10 User Roles and Permissions

Convert HTML to PDF using PHP

Convert HTML to PDF using PHP Dompdf Library

Show 313 comments

Comments are closed.

Javed Ur Rehman

I am Javed Ur Rehman, a Lead Software Engineer.

I build ecommerce websites, web applications, web portals, and online stores to help businesses to
grow.

If you are looking for the same. Contact me

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

Suresh Parwani From Panama

“Just wow! Happy customer here. Thumbs up!”

Albert Diaz From Manila, Philippines

Recent Posts

Laravel 11 Spatie User Roles and Permissions

Laravel 11 Custom User Registration & Login Tutorial

How to Create and Update Chart using JavaScript

Simple Laravel 11 CRUD Application Tutorial

Laravel 10 Livewire User Registration and Login

Laravel 10 Livewire CRUD Application Tutorial

Laravel 10 Custom Validation Rule Tutorial

Simple Laravel 10 Factory Tutorial

Laravel 10 REST API using Passport Authentication

Laravel 10 REST API using Sanctum Authentication


POPULAR POSTS

Insert, View, Edit and Delete Record from Database Using PHP and MySQLi

532.6k views | 313 comments

Simple User Registration & Login Script in PHP and MySQLi

476.6k views | 261 comments

Simple Shopping Cart using PHP and MySQL

283.3k views | 205 comments

Create and Consume Simple REST API in PHP

259.8k views | 149 comments

Forgot Password Recovery (Reset) using PHP and MySQL

197.2k views | 233 comments

Create Simple Pagination Using PHP and MySQLi

150.2k views | 136 comments

Laravel 10 Custom User Registration & Login Tutorial

92k views | 36 comments

Upload File Using PHP and Save in Directory

88.5k views | 65 comments

Create a Simple Captcha Script Using PHP

76.2k views | 74 comments

Select Box with Search Option using jQuery

58.6k views | 27 comments

AllPHPTricks is Designed and Developed by Javed Ur Rehman

Copyright © 2015 - 2025 All PHP Tricks. All Rights Reserved.


About Contact Resources Privacy Policy Terms and Conditions Disclaimer Sitemap

Go to Top

You might also like