[go: up one dir, main page]

0% found this document useful (0 votes)
168 views26 pages

The Best Guide To PHP CRUD Operations - Simplilearn

This document provides a tutorial on performing CRUD (create, read, update, delete) operations in PHP with MySQL. It explains how to create a database connection in PHP, create records by inserting data into a database table, read/view records by querying the database, update records by modifying data in a table, and delete records by removing data from a table. Code examples are provided for each CRUD operation. The goal is to help the reader understand how to perform common data management tasks using PHP and MySQL.

Uploaded by

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

The Best Guide To PHP CRUD Operations - Simplilearn

This document provides a tutorial on performing CRUD (create, read, update, delete) operations in PHP with MySQL. It explains how to create a database connection in PHP, create records by inserting data into a database table, read/view records by querying the database, update records by modifying data in a table, and delete records by removing data from a table. Code examples are provided for each CRUD operation. The goal is to help the reader understand how to perform common data management tasks using PHP and MySQL.

Uploaded by

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

23/6/23, 9:35 The Best Guide to PHP CRUD Operations | Simplilearn

Software Development

Tutorials Articles Free Practice Tests On-demand Webinars

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

The Best Guide to PHP CRUD Operations You'll Ever Need

Lesson 6 of 11 By Ravikiran A S

Last updated on Feb 26, 2023 159743

Table of Contents

How to Create a MySQL Database Connection?

How to Create Records?

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?

How to Delete 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

You will now understand the different operations in detail.

Learn from the Best in the Industry!

Caltech PGP Full Stack Development

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

How to Create a MySQL Database Connection?

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.

Here, name the file as config.php

<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "mydb";

$conn = new mysqli($servername, $username, $password, $dbname);

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

die("Connection failed: " . $conn->connect_error);

?>

How to Create Records?

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.

Get All Your Questions Answered Here!

Caltech PGP Full Stack Development

EXPLORE PROGRAM

Name the file as create.php.

<?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'];

$sql = "INSERT INTO `users`(`firstname`, `lastname`, `email`, `password`, `gender`)


VALUES ('$first_name','$last_name','$email','$password','$gender')";

$result = $conn->query($sql);

if ($result == TRUE) {

echo "New record created successfully.";

}else{

echo "Error:". $sql . "<br>". $conn->error;

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>

<form action="" method="POST">

<fieldset>

<legend>Personal information:</legend>

First name:<br>

<input type="text" name="firstname">

<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

<input type="text" name="lastname">

<br>

Email:<br>

<input type="email" name="email">

<br>

Password:<br>

<input type="password" name="password">

<br>

Gender:<br>

<input type="radio" name="gender" value="Male">Male

<input type="radio" name="gender" value="Female">Female

<br><br>

<input type="submit" name="submit" value="submit">

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

Basics to Advanced - Learn It All!

Caltech PGP Full Stack Development

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

How to Read/View Records?

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

Now, name the page as view.php

<?php

include "config.php";

$sql = "SELECT * FROM users";

$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) {

while ($row = $result->fetch_assoc()) {

?>

<tr>

<td><?php echo $row['id']; ?></td>

<td><?php echo $row['firstname']; ?></td>

<td><?php echo $row['lastname']; ?></td>

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

<td><?php echo $row['gender']; ?></td>

<td><a class="btn btn-info" href="update.php?id=<?php echo $row['id']; ?


>">Edit</a>&nbsp;<a class="btn btn-danger" href="delete.php?id=<?php echo
$row['id']; ?>">Delete</a></td>

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

Create and Showcase Your Portfolio from Scratch!

Caltech PGP Full Stack Development

EXPLORE PROGRAM

How to Update Records?

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

$sql = "UPDATE `users` SET


`firstname`='$firstname',`lastname`='$lastname',`email`='$email',`password`='$passwo
rd',`gender`='$gender' WHERE `id`='$user_id'";

$result = $conn->query($sql);

if ($result == TRUE) {

echo "Record updated successfully.";

}else{

echo "Error:" . $sql . "<br>" . $conn->error;

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

$sql = "SELECT * FROM `users` WHERE `id`='$user_id'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while ($row = $result->fetch_assoc()) {

$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

?>

<h2>User Update Form</h2>

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

<fieldset>

<legend>Personal information:</legend>

First name:<br>

<input type="text" name="firstname" value="<?php echo $first_name; ?>">

<input type="hidden" name="user_id" value="<?php echo $id; ?>">

<br>

Last name:<br>

<input type="text" name="lastname" value="<?php echo $lastname; ?>">

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

<input type="password" name="password" value="<?php echo $password; ?>">

<br>

Gender:<br>

<input type="radio" name="gender" value="Male" <?php if($gender == 'Male'){


echo "checked";} ?> >Male

<input type="radio" name="gender" value="Female" <?php if($gender ==


'Female'){ echo "checked";} ?>>Female

<br><br>

<input type="submit" value="Update" name="update">

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

Become a Skilled Web Developer in Just 9 Months!

Caltech PGP Full Stack Development

EXPLORE PROGRAM

How to Delete Records?

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.

Now, name the file delete.php

<?php

include "config.php";

if (isset($_GET['id'])) {

$user_id = $_GET['id'];

$sql = "DELETE FROM `users` WHERE `id`='$user_id'";

$result = $conn->query($sql);

if ($result == TRUE) {

echo "Record deleted successfully.";

}else{

echo "Error:" . $sql . "<br>" . $conn->error;

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

?>

Get All Your Questions Answered Here!

Caltech PGP Full Stack Development

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.

You can refer here for a video tutorial on CRUD 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:

Name Date Plac

Cohort starts on 19th Jul 2023,

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

Cohort starts on 4th Aug 2023,


Full Stack Java Developer Your C
Weekend batch

Cohort starts on 25th Aug 2023,


Full Stack Java Developer Your C
Weekend batch

About the Author

Ravikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek


always in the hunt to learn the latest technologies. He is proficient with Java
Programming Language, Bi…

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

Full Stack Web Developer - MEAN Stack Lifetime


Access*
705 Learners

Automation Testing Masters Program Lifetime


Access*
881 Learners

*Lifetime access to high-quality, self-paced e-learning content.

Explore Category

Recommended Resources

Implementing Stacks in Data Blockch


Structures Compre

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

G Start the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

 6 Share Best Newes

Be the first to comment.

Subscribe Privacy Do Not Sell My Data

© 2009 -2023- Simplilearn Solutions

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

You might also like