What is SQL injection — and why the surname O'Brien breaks your site

Here's a funny thing. Someone types the surname O'Brien into your form — and the site throws an error. They're not a hacker. That's just their name.
Now the unpleasant part. That's exactly the hole whole databases leak through. With O'Brien it simply fired in a harmless way.
The database can't see where your code ends and data begins
Your app doesn't send the database an "object" or a "form". It sends one string of text.
When an AI agent writes you a quick query, it often comes out like this:
// never write this
const q = "SELECT * FROM users WHERE name = '" + name + "'";
Drop O'Brien in there and you get:
SELECT * FROM users WHERE name = 'O'Brien'
The database reads left to right. The first quote opened the text, the second closed it. What follows is Brien' — no longer text, just garbage in the middle of a command. Hence the error.
Now swap the surname for ' OR '1'='1. You get a condition that's always true. And the query honestly returns the entire table.
Why "strip the quotes" is a bad idea
The beginner's first instinct (and, honestly, a lot of AI answers): let's cut out apostrophes and the word DROP.
Don't. Three reasons.
- You break honest people. O'Brien, d'Artagnan, "Joe's Café" — apostrophes and quotes are real data.
- A list of forbidden things is never complete. There are
--comments, encodings,/**/instead of a space. You won't guess them all. - OWASP advises against it outright. Their SQL injection prevention guidance labels escaping STRONGLY DISCOURAGED and says the approach is "fragile compared to other defenses" and that they "CANNOT guarantee that this option will prevent all SQL injections in all situations".
Input validation is useful, but it's about junk and typos. Not about this hole.
A parameterized query: data never enters the command text
The real fix looks boring — and that's the whole point.
// this is correct
const q = "SELECT * FROM users WHERE name = $1";
await db.query(q, [name]);
Notice: name isn't glued onto the string. It travels separately, as a second argument.
The database parses the command first — "select from users where name equals this slot". The structure is fixed. Only then does the value go into the slot. Like cargo in a shipping container: the container's route is already set, the cargo can't change it.
As OWASP puts it, the database "will always distinguish between code and data, regardless of what user input is supplied".
So O'Brien is found normally. And so is ' OR '1'='1 — just as a person with a very strange name who isn't in the table.
Where parameters don't help
Some spots can't take a slot. Those are table names, column names and sort direction (ASC / DESC). The database needs them before it parses the query.
And that's exactly what tends to arrive from the interface: "sort by price", "sort by date".
For those cases OWASP recommends not escaping but mapping to a list of allowed values:
const columns = { price: "price", date: "created_at" };
const col = columns[req.query.sort] ?? "created_at";
The user doesn't pick a column. They pick a key from your dictionary. Everything else quietly becomes the default.
Second-order injection: stored clean, exploded later
There's a non-obvious variant that trips up even careful projects.
Someone registers under the name admin'--. The write goes through a parameter, all honest: the string lands in the database as ordinary text. Nothing breaks.
A week later you're writing a report and you pull that name out of the database — to glue it into another query. That's when it goes off.
The moral is simple and slightly annoying: data from your own database isn't trusted either. It came from outside once. The parameter rule applies to every query, not just the one sitting next to a form.
The good news: you're probably already safe
If you reach the database through an ORM or a client like Supabase, parameterization is built in — you'd have to work at gluing strings by hand.
The hole shows up in one place: when you (or an agent) write raw SQL for "a complex query the ORM can't do". Search your project for raw, execute, query( with a plus or a template string inside. That's your review list.
About scale, by the way. In the OWASP Top 10 2021 report, injection was tested on 94% of applications and produced 274,228 occurrences, with a max incidence rate of 19.09% per application. In the 2025 edition injection moved from third place down to fifth. Not because the hole disappeared — frameworks just started closing it by default.
Next, walk through the pre-launch security checklist — injection is one of its seven items.
How do I check whether I have SQL injection?
Type a single apostrophe ' into any search or login field and submit. If the app answers with a database error or a 500, your text reached the command. It shouldn't: the app should simply find nothing.
Does an ORM fully protect me from injection?
For ordinary queries, yes. But almost every ORM has a raw SQL mode, and there the old rule stands: pass values as parameters, not by gluing. More about the query language itself in our explainer on what SQL is.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





