SQL Injection

SQL injection is a security attack where malicious input is inserted into a SQL query, tricking the database into running unintended commands. It can expose or destroy data.

SQL

It happens when user input is pasted directly into a query string. An attacker can type SQL fragments — like ' OR '1'='1 — that change the query's meaning, bypassing logins or dumping entire tables.

The fix is to never build queries by concatenating user input. Use parameterized queries (prepared statements), which send the data separately from the SQL so it can never be treated as code.

Examples

SQL

-- UNSAFE: input glued into the query
-- "SELECT * FROM users WHERE name = '" + input + "'"
-- Attacker enters:  ' OR '1'='1

-- SAFE: parameterized query
SELECT * FROM users WHERE name = ?;

Output:

Parameterized values can't alter the query's logic.

Learn it in a lesson

See also

Back to the Dictionary