So I was messing around with SQL injections on my website, trying to find an easy way to protect it from SQL injections. I found many things about "Prepared SQL statements", mostly in this post.
This is all nice and yes, it does prevent any kind of SQL injection I know, and also prevents all applications made for testing the security from SQL injecting.
However, I found one thing in what all injections are the same, and that is using ' quotes. My question is: why use prepared statements or something like that, when I could simply prevent query from being run using htmlspecialchars?
Example:
$id = htmlspecialchars($_GET['id'], ENT_QUOTES);
$query = Mysqli_Query($dbc, "SELECT * FROM `Users` WHERE `user_id`='$id'") or die(mysql_error());
This prevented all the injections by changing the ' character into ', which is invalid in MYSQL statements.
Am I missing something in here? Is there a reason why people use prepared statements instead?