Engineering 4 min read Updated
Preventing SQL injection with prepared statements
In short
Send every user-supplied value to the database as a bound parameter, take table names and sort orders from fixed lists, and give the application only the rights it needs.
A search box, a login form or a filter. Text from a visitor goes straight into an SQL query and can change what the query means. The fix is simple, though: keep the structure of the query fixed and send the data to the database separately. Prepared statements in PHP do this well, but they do not solve everything, and the rest needs controls that limit the damage from a badly built query.
Injection starts where queries are built from strings
The vulnerable pattern is string building: visitor input is joined into the SQL text before the query is sent. A quote character in the input can close a string in the query and add conditions or statements that the developer never wrote. The database receives one string and cannot tell which parts came from the developer and which from the visitor. Login forms are not the only risk. Search fields, filters, sort options and admin tools can carry the same problem.
Send values as bound parameters
According to the PHP manual, a prepared statement is a compiled template for the SQL, customised with parameters. Values travel separately from the query text, and the driver handles quoting, so you never quote parameters by hand. PDO accepts named placeholders such as :name, as well as question marks. If the application sends every value as a parameter, SQL injection through those values does not occur. The same source warns that any part of the query built from unescaped input stays open.
A placeholder must stand in for a whole value. A pattern that puts percent signs inside the quotes around a placeholder is invalid. Write the placeholder alone, and add the percent signs to the value you pass in, as the manual’s example does.
Placeholders cannot bind table names or sort order
Placeholders bind values, not identifiers. A table name, a column name or a sort direction cannot be passed as a parameter, so it must come from the code. OWASP recommends mapping the user’s choice to a strict allowlist of values that the application owns. The table summarises this approach.
| Part of the query | Safe approach |
|---|---|
| Values in conditions, inserts and LIKE patterns | A placeholder, with the value passed separately |
| Table or column names | A fixed list in code, selected by a key the visitor chooses |
| Sort direction | A choice between two fixed strings, with anything else rejected |
| Database access | An account with only the rights the application needs |
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Validate, but keep binding
Input validation is useful. A number field should contain a number, and a sort option should be one of the known choices. But validation complements parameter binding and does not replace it. OWASP is explicit about this, so keep binding every value, even one that has passed your checks. A value that is safe today may later reach a query that is built differently.
Give the database account only the rights it needs
OWASP advises against giving application accounts DBA or admin rights. Each application should have its own account, with only the access it actually needs. If the application never creates or drops tables at runtime, its account should not hold those rights. A narrow account does not stop injection, but it limits what an attacker can do when a query goes wrong. The credentials for that account belong outside the code, as explained in keeping secrets out of the code.
A review checklist
- Search the codebase for query strings built with concatenation or variable interpolation.
- Confirm that every value reaches the database through a placeholder.
- Check that table names, column names and sort orders come only from fixed lists.
- Verify that the application account has no administrative rights.
- Test forms and filters with quote characters and SQL keywords, and confirm that the results do not change.
Output escaping is a separate problem, and the article Escaping output so that XSS cannot run covers it. For the wider set of risks a product owner should plan for, see The OWASP Top 10 for product owners.
The advice on identifiers, validation and privileges follows the OWASP cheat sheet. The checklist is a practical summary written for this article, not an official list.
Common questions
- Are prepared statements enough on their own?
- They stop injection through the values they bind. The PHP manual warns that any part of the query built from unescaped input stays open, and identifiers such as table names cannot be bound.
- Can a placeholder stand in for a table or column name?
- No. Placeholders bind values only, so map the choice made by the user to a fixed name in the code and reject anything outside that list.
- Does input validation make parameter binding unnecessary?
- No. OWASP says validation complements parameter binding and does not replace it, so keep binding values that have passed the checks.
Sources
- PHP: Prepared statements and stored procedures - Manual, PHP Group (php.net). Accessed 24 November 2026
- SQL Injection Prevention - OWASP Cheat Sheet Series, OWASP Foundation. Accessed 24 November 2026
Further reading
-
Escaping output so that XSS cannot run
Escape each value for the place where it is printed, because text that is safe in HTML can still run as code inside a script, a URL or CSS.
-
Keeping secrets out of your code and logs
Keep secrets in a store outside the code, give each one the smallest access it needs, and rotate it on schedule and after any leak.
-
The OWASP Top 10 for product owners
The OWASP Top 10 is an awareness list of the most critical web risks, and the 2025 edition gives ten categories you can turn into planning questions.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea