I've read about SQL injection so I tried it with my site and of course it worked.. I know that the solution is parameterized queries and I also know that there are a lot of examples out there but none of them mentions the part where we're connecting to the database. So here's a part of my login page's PHP code:
$userName = $_POST["username"];
$userPass = $_POST["password"];
$query = "SELECT * FROM users WHERE username = '$userName' AND password = '$userPass'";
$result = mysqli_query($dbc, $query); //$dbc is for MySQL connection: $dbc = @mysqli_connect($dbhost, $dbuser, $dbpass, $db)
$row = mysqli_fetch_array($result);
if(!$row){
echo "No existing user or wrong password.";
}
I've been looking for the solution for a long time but I just could not figure out how I could get it work in a parameterized way. Could you please help me how I should complete my code to prevent SQL injection?