PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
46: Building a Basic CRUD Application
We've spent the last few lessons talking about the individual components—how to connect to a database, how to handle POST requests, and how to write SQL. Now, it's time to actually glue it all together. We're going to build a simple Personal Book Collection Manager. It's a classic CRUD (Create, Read, Update, Delete) app, which is the backbone of almost every web application you'll ever build.
Setting up the Library Table
I don't like over-engineering things early on, so we'll keep the schema lean. We just need a table to hold our books. I'm using an auto-incrementing ID because trying to manage unique identifiers manually is a nightmare you don't want.
CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
year INT
);
Getting Books into the Database
For the "Create" part, we need a simple HTML form and a PHP script to process it. I usually put my logic at the top of the page to keep the HTML clean. Notice I'm using prepared statements here—I can't stress enough how important this is for security. Never, ever trust user input.
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_book'])) {
$stmt = $pdo->prepare("INSERT INTO books (title, author, year) VALUES (?, ?, ?)");
$stmt->execute([$_POST['title'], $_POST['author'], $_POST['year']]);
}
The form is straightforward: three inputs and a submit button. I'll name the button add_book so I can distinguish this request from others on the same page.
Displaying the Collection
Now for the "Read" part. This is the easiest bit. I'll fetch everything from the books table and loop through it. I prefer using a while loop with fetch() because it's more memory-efficient than pulling a massive array into memory if your library grows to thousands of books.
$stmt = $pdo->query("SELECT * FROM books");
while ($row = $stmt->fetch()) {
echo "<div>";
echo "<strong>" . htmlspecialchars($row['title']) . "</strong> by " . htmlspecialchars($row['author']);
echo " <a href='edit.php?id=" . $row['id'] . "'>Edit</a>";
echo " <a href='delete.php?id=" . $row['id'] . "'>Delete</a>";
echo "</div>";
}
Tweaking the Update Logic and Fixing a Refresh Bug
Updating a record requires two steps: first, we fetch the existing data to fill a form, and then we save the changes. Here is where I actually tripped up when I first wrote this. I wrote the update logic, tested it, and it worked. But then I hit "Refresh" in my browser after submitting the form, and I realized it sent the POST request again. In a more complex app, this could cause duplicate entries or weird state issues.
To fix this, I implemented the Post-Redirect-Get (PRG) pattern. Instead of just letting the page render after the update, I'll send a header('Location: ...') redirect. This clears the POST data and sends the user back to the list.
// Inside edit.php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $stmt = $pdo->prepare("UPDATE books SET title = ?, author = ?, year = ? WHERE id = ?"); $stmt->execute([$_POST['title'], $_POST['author'], $_POST['year'], $_POST['id']]); // My mistake was omitting this line originally: header("Location: index.php"); exit; }Clearing out the Old Stuff
Finally, we have "Delete". I'm using a simple GET request for this example because it's a small internal tool. In a production environment, you'd probably want a confirmation dialog or even a POST request to prevent accidental deletions from search engine crawlers or mistaken clicks.
if (isset($_GET['id'])) { $stmt = $pdo->prepare("DELETE FROM books WHERE id = ?"); $stmt->execute([$_GET['id']]); header("Location: index.php"); exit; }And that's it. We've built a full loop: adding data, viewing it, modifying it, and removing it. It's not fancy, but it's the foundation of almost everything you'll do in PHP web development.
📋 Practical Task
Adding a "Genre" Field to the Library Manager
Now it's your turn to extend the application. The current Book Collection Manager is too simple; we need to know what kind of books we have.
- Modify your
bookstable to add agenrecolumn (VARCHAR). - Update the "Add Book" form and the corresponding
INSERTlogic to handle the new genre field. - Update the "Edit" page so you can change the genre of an existing book.
- Display the genre next to the author in the main book list.
There are no comments for now.