Building a Small CRUD App
Site Admin
· 11 Sep 2026
· 7 views
Building a Small CRUD App
Bring PDO, forms, and functions together in a minimal item manager: create, read, update, and delete records from a MySQL table.
The database table
CREATE TABLE items (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(8,2) DEFAULT 0
);
Shared helpers
function db(): PDO
{
return new PDO('mysql:host=localhost;dbname=shop', 'root', '');
}
Create and read
// insert.php
$stmt = db()->prepare('INSERT INTO items (name, price) VALUES (?, ?)');
$stmt->execute([$_POST['name'], $_POST['price']]);
// list.php
$rows = db()->query('SELECT * FROM items')->fetchAll();
foreach ($rows as $row) {
echo $row['name'] . ' ' . $row['price'];
}
Update and delete
// update.php
$stmt = db()->prepare('UPDATE items SET price = ? WHERE id = ?');
$stmt->execute([$_POST['price'], $_GET['id']]);
// delete.php
db()->prepare('DELETE FROM items WHERE id = ?')->execute([$_GET['id']]);
Bringing it together
Each screen is a small PHP file: a list shows rows with edit and delete links, a form posts new data, and update reuses the same form with the record's current values. Redirect after writes so refreshing the page does not resubmit:
header('Location: list.php');
exit;
Keep it secure
Validate and sanitize every value, use prepared statements everywhere, and escape name output with htmlspecialchars. The same skeleton scales into bigger apps by adding auth, pagination, and layout files.
Key Points
- One table, one pattern: prepared CRUD statements.
- Fetch results with fetchAll and loop over rows.
- Update and delete target rows by id.
- Redirect after writes to prevent resubmission.
- Prepared statements and escaping keep it safe.