A Worked Scenario: SQL Injection in a Student Guestbook
This is a made-up scenario for learning. It follows an invented student, Learner B, through a common first-project mistake: building SQL by gluing strings together. No real site or person is involved.
The project
Learner B builds a guestbook for a class website. Visitors type their name and a message; the page lists all messages. The code, in Python with SQLite, looks like this:
name = form["name"]
message = form["message"]
db.execute("INSERT INTO entries (name, message) VALUES ('" + name + "', '" + message + "')")
It works for every test Learner B tries.
The first clue
A classmate testing the page types a message containing an apostrophe: It's great!. The page shows a database error instead of saving the message. The apostrophe closed the text early, and the rest of the message became broken SQL.
That error is the warning sign. If an apostrophe can break the query, other carefully chosen text could change what the query does.
Understanding the risk
On a practice copy of the app, the teacher shows how a crafted message could add extra SQL after the closing quote. In a guestbook that might mean inserting fake entries or, with other queries on the site, reading data that should be private. The exact damage depends on the database and the query, but the cause is always the same: user text treated as part of the command.
The fix
Learner B changes the code to use placeholders:
db.execute(
"INSERT INTO entries (name, message) VALUES (?, ?)",
(form["name"], form["message"])
)
Now the SQL and the values travel separately. It's great! is saved exactly as typed, quote mark and all, and no text can change the command.
Checking the rest of the app
The guestbook also has a page that shows entries by one visitor, using a URL like /entries?name=Sam. Learner B finds the same glued-string pattern there and fixes it too. Then they use a printable injection-proofing planner to list every query in the app and tick off each one.
Decision points in the scenario
| Moment | Choice made | Why |
|---|---|---|
| Apostrophe error appears | Investigate rather than "strip apostrophes" | Removing characters hides the problem and changes people's messages |
| Fixing the insert | Use placeholders | The reliable, standard defence |
| After the fix | Check every other query | One fixed query does not make an app safe |
| Displaying messages | Escape HTML when showing them on the page | A separate problem: stopping messages from injecting page code |
The last row is worth noting. Placeholders protect the database. When the messages are displayed on a web page, the page must also escape them, so a message cannot add its own HTML or scripts. Most web frameworks do this by default in their templates.
What the scenario teaches
- An apostrophe breaking your app is a symptom of glued SQL.
- Placeholders fix the cause, not just the symptom.
- Check every query, not only the one that broke.
- Only test your own practice copy, never someone else's site.
For more practice, try Breaking and Fixing a Tiny Login Form.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative example: the organisations, people, projects and results described on this page are hypothetical. They do not describe a real client or engagement, and no consulting services are offered here.