To sanitize user input and prevent SQL injection, never build a database query by stitching user text into SQL, and never trust a value just because it came from your own form. Use parameterized queries, validate input against an expected shape, and encode output for its context. Those three habits close the most common injection holes in 2026, and they work in every mainstream stack, from PHP and Node to Python and Go.
SQL injection is old, but it is not gone. It survives wherever developers concatenate strings into queries, in legacy code, admin tools, reporting pages, and search features. Attackers probe these spots automatically, so a hole does not need a targeted attacker to be found. The fix is mostly about using your framework’s safe path by default.
How SQL injection happens
Picture a login query built like this: SELECT * FROM users WHERE username = ‘[input]’ AND password = ‘[input]’. If the input contains a quote and a crafted condition, the query’s meaning changes, and the attacker can log in without a password or dump rows they should never see. The database cannot tell which parts of the query were code and which were user data, because they arrived as one string.
Parameterized queries prevent sql injection by separating the two. The query goes to the database with placeholders, and the values travel separately, so the database treats them as data only, never as code. Prepared statements in PHP PDO, parameterized queries in Python DB-API, and query builders that parameterize by default all give you this separation. Use them for every query that touches user input, including search terms, filters, sort fields, and IDs from URLs.
Sanitize user input: validate, then parameterize
When you sanitize user input, start with validation: define what a value should look like, and reject the rest. An age field should be a number in a sane range. A sort field should match one of a few allowed column names, never raw text from the request. An ID should be an integer or a UUID pattern. Input validation allowlist rules cover the structural parts: sort columns and directions come from fixed lists. Validation narrows what reaches the database, and it also improves error messages and data quality.
Validation alone is not enough, because valid-looking input can still be hostile: a name field can legitimately contain an apostrophe, and a search box can legitimately contain quotes. That is why parameterization is the mandatory layer and validation is the supporting layer. Escaping by hand is the fragile middle path: it works until a developer forgets one spot, uses the wrong escaping function for the database, or changes character sets. Prefer the database driver’s own parameter binding over manual escaping everywhere.
Stored procedures do not automatically save you. A procedure that builds dynamic SQL from its arguments by concatenation is just as vulnerable as application code. If you use procedures, parameterize inside them too, and avoid dynamic SQL unless you have a reviewed reason for it.
Beyond the query: output, files, and second-order injection
Injection is not only a database problem. User input that reaches a page must be encoded for HTML context, or it becomes cross-site scripting. Input that reaches a shell command, a file path, or an LDAP query needs its own safe handling. Treat every sink, every place input is used, as needing its own protection: the escaping that is right for SQL is wrong for HTML, and vice versa.
Second order sql injection deserves a mention. Data stored safely once can turn hostile later, when another part of the app reads it back and builds a query without parameters. The defence is the same rule applied everywhere: parameterize at the point of use, every time, not only at the point of entry. Code review should flag any query built by concatenation, wherever it appears.
ORMs and query builders help, but they are not magic. Raw query methods, string-based ordering, and dynamic column names inside an ORM can reopen the hole. When you must use a raw fragment, allowlist the variable parts: column names from a fixed list, directions limited to ASC or DESC, and values still passed as parameters.
Defence in depth for database access
Layered defences limit damage when one layer fails. Give the application’s database account only the permissions it needs: a web app rarely needs DROP or ALTER rights, and a reporting account should read only the tables it reports on. That is least privilege applied to the database, and it turns a successful injection from a full takeover into a limited read. A guide to role-based access design shows the same thinking for infrastructure roles.
A web application firewall adds another layer by blocking common injection patterns before they reach your app. A comparison of WAF options for small sites covers the trade-offs, but treat a WAF as a safety net, not a fix: attackers adapt patterns, and the query layer must be safe on its own. Keep dependencies updated too, since database drivers and frameworks occasionally fix escaping flaws, and CI/CD hardening helps those updates ship safely.
For the database itself, know your engine’s quirks. A comparison of MySQL and PostgreSQL tuning is about performance, but the underlying point transfers: each engine has its own placeholder syntax and escaping rules, so test parameterization against your actual database, not in theory.
A checklist for your next code review
- Every query with user input uses parameters, with no string concatenation of values.
- Sort columns, table names, and other structural parts come from an allowlist.
- Validation rejects values outside the expected shape before they reach the query.
- Output is encoded for its context: HTML, attribute, JavaScript, or URL, as appropriate.
- The database account holds the minimum permissions the app needs.
- Raw query methods in ORMs are reviewed as carefully as hand-written SQL.
Run that checklist on your most exposed endpoints first: login, search, filters, and any public form. Fixing injection is unglamorous work, but it is also one of the highest-value fixes in web security, because the safe pattern is simple, the tests are easy to write, and the hole it closes is one attackers still find every day.