SQL Injection
when a form field becomes a database command
SQL injection happens when an application builds a database query by gluing user input into a string, so an attacker can change the meaning of the query. It is one of the oldest web flaws and is still found today.[1]
A simple picture
Imagine a login that builds this query from what you type:
SELECT * FROM users WHERE name = '<input>' AND password = '<input>'
If the name field is accepted as raw text, an attacker can type a value that closes the quote and adds their own logic, for example making the condition always true. The database cannot tell which part was meant as data and which part is an instruction, because they arrive mixed together in one string.
What attackers can do with it
- Read data they should not see, including other users' records.
- Bypass logins.
- Change or delete data.
- In some setups, reach the underlying server.
Variants include in-band (results are visible in the page), blind (the attacker infers answers from true or false behavior or from timing) and out-of-band (data is sent through another channel).
The fix: separate code from data
The reliable defense is parameterized queries, also called prepared statements. The query structure is sent first, and the values are passed separately, so input can never be interpreted as SQL:[2]
SELECT * FROM users WHERE name = ? AND password_hash = ?
Other layers that help but do not replace it:
- ORM and framework features that parameterize for you, used carefully (raw query escapes bring the risk back).
- Input validation with allowlists, particularly for things that cannot be parameterized such as column names.
- Least privilege for the database account, so a flaw cannot drop tables or read everything.
- Not showing raw database errors to users.
- A web application firewall can block known patterns but is a safety net, not a fix.
Testing safely
Only test applications you own or are authorized to test. Deliberately vulnerable practice apps and learning platforms exist for this purpose.
Sources
Test yourself
My notes
Private — saved only on this device, never sent anywhere.