CHAPTER 27
Beginner
CRUD Application with PHP and MySQL
Updated: May 16, 2026
5 min read
# CHAPTER 27
CRUD Application with PHP and MySQL
1. Introduction
A database without an interface is invisible. A web application without a database is amnesiac. Combining them creates fully functional software. Every major web application—from Twitter to WordPress—is essentially a CRUD application. They allow users to Create posts, Read feeds, Update profiles, and Delete comments. In this chapter, we will build the core PHP logic for a complete User Management system.2. Learning Objectives
By the end of this chapter, you will be able to:- Architect the folder structure for a PHP CRUD app.
- Implement the "Create" logic to insert new records.
- Implement the "Read" logic to fetch and display records in an HTML table.
- Implement the "Update" logic to modify existing data.
- Implement the "Delete" logic to remove records securely.
3. The Setup (database.php)
First, we isolate our PDO connection logic into a single file so we can include it anywhere.
database.php
4. Create (Inserting Data)
We need an HTML form that submits viaPOST. When the form is submitted, we capture the data and use a Prepared Statement to safely insert it.
create.php
5. Read (Displaying Data)
Now we want to view all the users we created. We query the database and loop through the array to generate an HTML table.
read.php
6. Update (Modifying Data)
Updating requires two steps: First, fetch the existing user's data to pre-fill the form. Second, save the newly submitted data.
update.php
7. Delete (Removing Data)
Deletion is the simplest, but the most dangerous. Always use a Prepared Statement, even if the ID is just coming from the URL!
delete.php
8. Common Mistakes
-
Trusting
$_GETVariables: In thedelete.phpscript, the ID comes from the URL ($_GET['id']). Beginners often concatenate this directly into the SQL string because "it's just a number." Hackers can easily type text or malicious SQL into the URL bar. ALWAYS use Prepared Statements for$_GETvariables!
-
Missing
htmlspecialchars(): If a malicious user types<script>alert('Hacked!');</script>into the Name field oncreate.php, and you echo it out directly onread.php, the script will run in everyone's browser.htmlspecialchars()converts the HTML brackets into safe text entities, neutralizing the attack.
9. Best Practices
-
Post/Redirect/Get (PRG) Pattern: In
create.php, if a user clicks "Refresh" after submitting the form, their browser will submit the data again, creating a duplicate row! To prevent this, after the$stmt->execute(), you should immediately redirect them away usingheader("Location: read.php");.
10. Exercises
-
1.
What does the acronym CRUD stand for, and which fundamental SQL commands (
INSERT,SELECT,UPDATE,DELETE) correspond to each letter?
-
2.
Why must you wrap database outputs in
htmlspecialchars()before displaying them in an HTML table?
11. MCQ Quiz with Answers
Question 1
In the "Read" portion of a CRUD app, what PDO method is used to retrieve all the rows from the executed query and store them in a PHP Array?
Question 2
When building a "Delete" feature, why is it critical to use a Prepared Statement even if the ID is simply being passed via the URL ($_GET)?
12. Interview Questions
- Q: Explain the Post/Redirect/Get (PRG) pattern and how it prevents duplicate row insertions in a standard PHP/MySQL CRUD application.
-
Q: Walk me through the security vulnerabilities of taking an ID from the URL (
delete.php?id=5) and concatenating it directly into aDELETEquery string.
13. FAQs
Q: Should I put all my CRUD logic in one file or multiple files? A: For learning, separating them intocreate.php, read.php, etc., is best. In professional environments, developers use an MVC Framework (like Laravel or Symfony) that routes all actions through a single "Controller" file.
14. Summary
You have successfully built the engine of the web. By combining thedatabase.php connection logic with the four fundamental SQL operations (INSERT, SELECT, UPDATE, DELETE), and securing every single step with Prepared Statements, you now possess the skills to build almost any basic web application from scratch.