No, you don't need to enter the correct username and password. Here are the steps:
First, ensure that you use an inline query in your PHP. Something like this:
sql = "SELECT * FROM accounts WHERE username = '$username' AND password = '$password'";
Second, ensure the $username
and $password
variables are getting their values directly from the username and password textboxes.
Now you can test an SQL injection attack by entering any value in the username textbox and something like this in the password textbox:
x';delete from accounts where '0'='0
This will delete all records from your accounts
table. You can enter any other SQL instead if you like to test more.
EDIT Just to address some opinions that understanding how SQL injection works is not necessary as long as you use parameterized queries.
This is a real issue that I've seen in other topics. Knowing how things work is absolutely essential for any "good" programmer. It is not enough to know how you should do things, you cannot be a "good" programmer if you don't know why you're doing them that way and how they work. So yes, knowing how SQL injection, XSS, CSRF, etc attacks work is a must for any experienced programmer.
It is really sad to see all those experienced programmers telling the novice ones that they don't really need to understand. Why? Do they want to be the only ones who know? Do they think that others aren't smart enough? I don't know, but this is not the spirit of StackOverflow, to say the least, thus I think that such statements should be flagged and removed.
Also, parameterized queries don't prevent all kinds of SQL injection, because not everything can be parameterized (example, columns & table names, arrays of values, etc), but by knowing how SQL injection works, you'll know what to do in those situations. Take this query as an example:
SELECT * FROM students WHERE status IN(,,,,)
If the array has a known number of values, then you can send them as parameters, but if you really must have an unknown number of values, then you need to know how to protect your query. And you cannot protect your query unless you understand what you're up against. In this case you can create the parameters in a loop, but you must know how to do it correctly.
Here is a question on SO with good answers on the subject.