Input Validation vs Parameterized Query
People think checking what users type is enough to stop SQL injection, and it is not. Input validation turns away data that has the wrong shape, like letters in an age box. But a name can still hold a quote. A parameterized query keeps every value apart from the command, so even odd text stays data. You want both.
Input Validation
Making sure what users send fits the rules. Then your code uses it.
Input validation checks outside data against rules before your code uses it. An age should be a whole number between 0 and 150. An email should look like an email. Anything that breaks the rules is turned away with a clear message. Checks in the browser are only for comfort; the server must check again, because anyone can skip the page.
Parameterized Query
A query with blank spots for the data. What a user types can never become a command.
A parameterized query keeps the SQL command and the user's data apart. You write the query with blank spots, like ? or $1. The data is sent on its own and fills those spots. The database never treats that data as part of the command. This is the main defence against SQL injection, and almost every database library supports it.
In one line
- Input Validation
- Making sure what users send fits the rules. Then your code uses it.
- Parameterized Query
- A query with blank spots for the data. What a user types can never become a command.