In one lesson you will build a MySQL database yourself in phpMyAdmin, then connect a two-page website to it: a list of books and a form that adds new books.
Form 6 students want to swap their old textbooks. You are building the Book Swap site: anyone can see what books are on offer, and add a book they are willing to swap.
Every step on real phpMyAdmin and Book Swap screens, with captions. Use the chapters to jump back to a step while you work.
| Time | Work | You will need |
|---|---|---|
| About 60–70 minutes | Individually or in pairs | A Windows PC with WampServer, a code editor (Notepad++ or VS Code) and the starter kit |
http://localhost/phpmyadmin. Log in as root with no password and choose the MySQL server.bookswap, choose the collation utf8mb4_unicode_ci, and click Create.books, set Number of columns to 6, and click Create (or Go).| Name | Type | Length/Values | Default | Null | Index | A_I | What it stores |
|---|---|---|---|---|---|---|---|
book_id | INT | None | PRIMARY | ✓ | A unique number for each book | ||
title | VARCHAR | 100 | None | Book title (required) | |||
author | VARCHAR | 80 | NULL | ✓ | Author (optional, so Null is ticked) | ||
subject | VARCHAR | 40 | None | e.g. Biology | |||
owner | VARCHAR | 60 | None | Student lending the book | |||
available | TINYINT | 1 | As defined: 1 | 1 = available, 0 = swapped |
Tips. Ticking A_I (auto increment) on book_id also makes it the PRIMARY key, so MySQL numbers each new book for you. For available, choose As defined under Default and type 1.
Check: open the table’s Structure tab. You should see six columns, a key icon beside book_id, and AUTO_INCREMENT in its Extra column. Take your screenshot.
book_id empty, because MySQL numbers it. available is already filled in with 1 (its default), so leave that too. Click Go after each book.book_id and available = 1.| # | Task |
|---|---|
| 1 | List the title and owner of every book in one subject you entered (e.g. Biology). |
| 2 | Count how many books there are in each subject. |
| 3 | Mark one of your books as swapped (set available to 0) using its book_id. |
C:\wamp64\www\, so that you have C:\wamp64\www\bookswap\index.php.http://localhost/bookswap/. The page loads but says 0 book(s) listed. It is connected, but not reading anything yet. (A connection error? Check WAMP is green and the database is named bookswap. If you used MariaDB, change the port in includes/db.php to 3307.)bookswap folder in your editor and complete the three TODOs.How the files fit together: includes/db.php opens the connection to MySQL (it is finished; read it). index.php lists the books. add.php shows the form and saves new books. style.css styles both pages.
Two patterns you will need:
// Reading rows: run a query, then fetch every row into an array
$rows = $pdo->query('SELECT title FROM books')->fetchAll();
foreach ($rows as $r) { echo e($r['title']); }
// Using anything the visitor typed: a prepared statement with ? placeholders
$stmt = $pdo->prepare('SELECT * FROM books WHERE owner = ?');
$stmt->execute([$owner]);
index.phpHint: copy the reading pattern for the “all books” case, and use ORDER BY book_id DESC for newest first. For a chosen subject, use the prepared-statement pattern with WHERE subject = ?.
index.phpHint: use <?php foreach ($books as $b): ?> … <?php endforeach; ?> around one <tr>. Print each value with <?= e($b['title']) ?>.
add.phpHint: INSERT INTO books (title, author, subject, owner) VALUES (?, ?, ?, ?), then $stmt->execute([...]). You don’t insert book_id or available, because the database fills them in. After saving, send the visitor back with header('Location: index.php?added=1'); exit;
| # | Test | Expected | ✓/✗ |
|---|---|---|---|
| 1 | Open the home page | Your three books are listed, newest first, and the swapped one shows “Swapped” | |
| 2 | Add a book with all fields filled in | “Your book was added”, and the book appears at the top | |
| 3 | Add a book with no title | An error message appears and nothing is saved | |
| 4 | Add a book called O’Brien’s Notes | Saved and shown correctly (the apostrophe doesn’t break anything) | |
| 5 | Type a subject in the “Show subject” box and press Enter | Only that subject’s books are listed | |
| 6 | Refresh phpMyAdmin’s Browse tab | The books you added through the website are in the table |
swap.phpThen add this button to the last cell of each row in TODO 2, so every available book can be marked as swapped:
<form method="post" action="swap.php" style="display:inline"> <input type="hidden" name="book_id" value="<?= e($b['book_id']) ?>"> <button class="small" type="submit">Mark swapped</button> </form>
book_id the primary key, and why is it set to AUTO_INCREMENT?add.php use a prepared statement instead of putting the visitor’s text straight into the SQL?includes/db.php, which every page includes?index.php?Finished early? Try the full practical, Sweet Escape Goes Live, with a menu, an order form with validation and a staff report.