The Best Guide To PHP CRUD Operations - Simplilearn
The Best Guide To PHP CRUD Operations - Simplilearn
Software Development
Home Resources Software Development PHP Tutorial The Best Guide to PHP CRUD
Operations You'll Ever Need
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 1/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
Lesson 6 of 11 By Ravikiran A S
Table of Contents
ED
How to Read/View Records?
N
EA
CL
H t U d t R d ?
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 2/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
How to Update Records?
Conclusion
CRUD Operations are typically performed on databases, hence, in this PHP CRUD
Operations tutorial, you will implement CRUD techniques on MySQL databases with
the help of PHP.
The CRUD acronym comprises all the major operations that are performed on a
relational database. It stands for:
C = Create
R = Read
U = Update
D = Delete
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 3/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
EXPLORE PROGRAM
First, create a connection between the database and your PHP code.
The following code acts as the connection between the webpage and the database
where the data from the webpage will be stored.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
if ($conn->connect_error) {
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 4/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
?>
The first operation in PHP CRUD Operations, Create, is responsible for creating tables
or new records into an existing table. To do that, first, you must write the code for the
webpage to create an entry in the database.
EXPLORE PROGRAM
<?php
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 5/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
include "config.php";
if (isset($_POST['submit'])) {
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$result = $conn->query($sql);
if ($result == TRUE) {
}else{
ED
N
}
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 6/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
$conn->close();
?>
<!DOCTYPE html>
<html>
<body>
<h2>Signup Form</h2>
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<br>
Last name:<br>
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 7/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
<br>
Email:<br>
<br>
Password:<br>
<br>
Gender:<br>
<br><br>
</fieldset>
ED
N
EA
CL
</form>
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 8/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
</form>
</body>
</html>
This page displays a signup form that stores the details entered on the page into the
table named ‘users’.
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 9/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
EXPLORE PROGRAM
The second operation, just as the name suggests, ‘Read’ is used to display or read the
data that is already available in the database.
To perform the operation, you need to create a page that displays the records from the
table ‘users’.
<?php
include "config.php";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
ED
N
EA
<html>
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 10/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
<head>
<title>View Page</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>users</h2>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
ED
N
EA
CL
<th>Email</th>
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 11/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
<th>Gender</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
?>
<tr>
ED
N
EA
<td><?php echo $row['email']; ?></td>
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 12/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
</tr>
<?php }
?>
</tbody>
</table>
</div>
</body>
</html>
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 13/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
EXPLORE PROGRAM
The third operation i.e, ‘update’ is used to change or modify the already existing data
present in the database.
To do this, you need to create another page to update the details in the database.
Here, name the page as update.php
<?php
ED
include "config.php";
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 14/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
if (isset($_POST['update'])) {
$firstname = $_POST['firstname'];
$user_id = $_POST['user_id'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$result = $conn->query($sql);
if ($result == TRUE) {
}else{
ED
N
EA
CL
}
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 15/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
}
if (isset($_GET['id'])) {
$user_id = $_GET['id'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$first_name = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$password = $row['password'];
$gender = $row['gender'];
$id = $row['id'];
ED
N
EA
CL
}
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 16/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
?>
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<br>
Last name:<br>
<br>
Email:<br>
ED
N
EA
<input type="email" name="email" value="<?php echo $email; ?>">
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 17/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
<br>
Password:<br>
<br>
Gender:<br>
<br><br>
</fieldset>
</form>
</body>
ED
</html>
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 18/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
<?php
} else{
header('Location: view.php');
?>
In the update form, we need to select the user id which we want to update. You can
notice that the user id being updated is visible in the URL of the update page in the
picture below.
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 19/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
You can view the updated details by opening the view.php webpage.
EXPLORE PROGRAM
The last operation of CRUD is Delete and just as the name suggests, it is used to
delete an existing entry or table.
ED
N
EA
CL
To perform this operation, you must create a page that would let you choose the data
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 20/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
p p ,y p g y
entry that you want to delete from the database.
<?php
include "config.php";
if (isset($_GET['id'])) {
$user_id = $_GET['id'];
$result = $conn->query($sql);
if ($result == TRUE) {
}else{
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 21/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
?>
EXPLORE PROGRAM
ED
N
EA
CL
Conclusion
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 22/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
Co c us o
This brings us to the end of the “PHP CRUD Operations'' tutorial. In this, you have
learned how to perform CRUD operations on a database with the help of PHP by
creating, reading, updating, and deleting records using different web pages. Finally,
you created a config.php file to connect the web pages with the database to perform
the operations.
Are you planning to take the plunge and do a course on PHP? In that case,
Simplilearn’s PHP course would be an excellent choice. The Post Graduate Program in
Full Stack Web Development covers all the fundamental and advanced concepts in
PHP, making your journey towards learning PHP an easy one.
Previous Next
If you have any queries regarding PHP CRUD Operations, do mention them in the
Tutorial Playlist
comment section of this tutorial, and we’ll have our experts answer them for you.
Happy Learning!
Find our Full Stack Java Developer Online Bootcamp in top cities:
ED
Full Stack Java Developer Your C
N
Weekend batch
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 23/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
Ravikiran A S
View More
Recommended Programs
ED
N
EA
CL
Full Full Stack Java Developer Career Bootcamp
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 24/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
Full Full Stack Java Developer Career Bootcamp Lifetime
Stack Access*
405 Learners
J
Explore Category
Recommended Resources
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 25/26
23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn
0 Comments
1
Name
Disclaimer
PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management
Institute, Inc.
ED
N
EA
CL
https://www.simplilearn.com/tutorials/php-tutorial/php-crud-operations 26/26