SQL Injection Still Isn't Dead: Prepared Statements and Beyond

Last month, on a project we were reviewing ahead of a maintenance handover, I typed a single quote into the search box out of habit. A long SQL error spilled onto the screen: table names, query text, version info — all of it right there, on display. At that moment a teammate asked, "Boss, what year is it?" The year is 2023. The attack was first described in 1998. In other words, a class of vulnerability approaching a quarter of a century old is still sitting in the project in front of us, on a live system.

Let me be frank: the fact that SQL injection is still among the most common vulnerabilities in 2023 is an embarrassment for all of us as an industry. The fix is known, it's cheap, and it's been at our fingertips for thirty years. This post is an attempt to explain that fix once more — but this time with its edge cases.

The door a single quote opens

The root of the problem fits in one sentence: pasting user-supplied data directly into the query text. Here's the classic example: "SELECT * FROM uye WHERE ad='" . $_GET['ad'] . "'". Here, what the user types doesn't stay data; it becomes part of the query. The attacker adds their own SQL to the input, and your query turns into theirs.

I can almost hear the "who'd bother with our site" defense. Nobody bothers; tools do. Free tools like sqlmap automatically scan a form or a URL parameter, find the hole in seconds, and then pull the database out table by table. Your member list, your password hashes, your customer data, your order history — all the operator does is type the command and sip their coffee. The person behind it doesn't even need to be skilled; the whole thing has been industrialized.

And there's a side that keeps it fresh: as applications become APIs, the same mistake comes back in new disguises. In the endpoint behind a mobile app, in the filter parameter of a reporting screen, in the admin service that's "only called internally anyway"... Assuming there's no input just because there's no visible form field is one of the misconceptions we've corrected most often this year. An attacker doesn't see your form, they see your traffic; wherever there's a parameter, that's where the probing goes.

And in the age of KVKK (Turkey's personal data protection law), the bill for this is no longer just reputation. Data breach notification, administrative fines, lost customers... The chain of consequences from one single-quote oversight is long.

Separating data from commands: that's the whole story

The fix is called a parameterized query — in the PHP world, the prepare() + execute() pair in PDO. The logic is this: you send the skeleton of the query first and bind the data separately. The database engine never mixes the two; whatever the user types into the input, it's processed as data, not as a command. Injection doesn't just get harder — it becomes structurally impossible.

With string concatenation the input becomes part of the query; with a parameterized query in PDO the skeleton and the data travel separately, making injection impossible.
Separating data from commands is the whole story.

There's an objection we hear a lot here: "We already escape our inputs." Don't rely on escaping functions. Cases where escaping was bypassed through character set tricks are well documented; escaping is a safeguard whose correctness depends on a human, and humans forget. With a parameterized query, there's nothing to forget. Our rule is clear and has no exceptions: a dynamic value never enters a query through string concatenation. At our shop, violating this rule is an automatic rejection in code review.

(If you use an ORM, this is usually done on your behalf — but ORMs have "raw query" doors, and in projects with an ORM, that's almost always where the injections live. Anyone writing raw queries is within the scope of this post.)

There's also a lesser-known cousin: second-order injection. The input is harmless when it's first stored; but when that stored value is later trusted and used in another query via string concatenation, that's where the bomb goes off. That's exactly why the assumption "data coming from the database is clean" is wrong — the rule looks at how the data is used, not where it came from: every dynamic value that goes into a query, wherever it comes from, is bound as a parameter. No exceptions.

A footnote, too, for the belief that "we use stored procedures, so this doesn't apply to us": if a dynamic SQL string is being built inside the procedure, the injection has simply moved one floor down. A procedure isn't an alternative to a properly parameterized query; at best it's an envelope that contains one. Look at what's inside the envelope, not the envelope.

The corners prepared statements can't reach

Now for the part that takes real craftsmanship, because there are three corners where people who say "we used prepare, done" get stuck.

The first is dynamic column and table names. In the "build the ORDER BY on whichever column the user wants to sort by" scenario, the column name can't be parameterized — that's simply the nature of SQL. The fix: an allowlist. You keep the permitted column names in a fixed list in code and match what comes from the user against that list. If it's not on the list, it falls back to the default. Any attempt to "sanitize" text from the input and use it as a column name will be breached sooner or later.

The second is LIMIT and OFFSET values. Force them to integers (cast them), then bind them. The assumption "it's a number anyway" breaks on the first input that isn't a number.

The third is LIKE patterns — the most overlooked of all. The percent and underscore characters act as wildcards inside LIKE; if you don't escape them separately in user input, a different problem arises even without injection: someone who types a string of percent signs into the search box can force your database into table scans and slow the system down. We call it a performance DoS; it may not count as a security vulnerability, but the phone that rings at midnight is the same phone.

Three corners where prepared statements are not enough: dynamic column and table names, LIMIT and OFFSET values, and LIKE patterns.
Each corner has its own fix.

Who limits the damage if someone gets in

Everything I've described so far is the first line of defense. Good engineering also plans for what happens if the first line is breached — this is called defense in depth.

The most effective step is to restrict the privileges of the application's database user. There's usually no reason at all for the user the web application connects with to have DROP, ALTER, or even DELETE privileges on most tables — yet in most of the projects we inherit, the web user walks around like root's sibling with full privileges. Once privileges are restricted, the attacker's room to maneuver shrinks even in the worst-case scenario.

It's also possible to take this out of the realm of individual vigilance. Static analysis tools can automatically catch variables that enter a query via string concatenation; you add a rule to the CI pipeline, and code containing a violation lights up red before it even reaches review. We've had this rule active for two years, and I'll admit it occasionally catches even our senior engineers — a "temporary" report query written in a hurry can come from the most senior keyboard too. The tool does the work and the hand takes the credit, as the saying goes; but without the tool, the hand gets tired.

Second is error hygiene: SQL errors are written to the log, not shown to the user. Printing the error on screen, like the project at the start of this post, is handing the attacker a map of your database schema as a gift. Third is a WAF — a web application firewall — but in its proper place: as the last line of defense. An architecture that makes the WAF the first and only safeguard is like fitting a steel door to a building with a rotten foundation.

Layers of defense in depth: parameterized queries, restricted database privileges, static analysis, error hygiene, and a WAF as the last line.
Each layer is there in case the one before it is breached.

And be sure to test yourself. Running sqlmap against your own site (with your own permission, in a test environment) is half a day's work and the cheapest way to act before an attacker does. If you have a system you've inherited or one nobody has touched in years, and the question "I wonder what our situation is" is gnawing at you, get in touch — we run this scan as standard on every project we take over for maintenance, and by now we can almost predict what we'll find.

The single-quote test is free. Facing its results isn't.