SQL injection is a a vulnerability that allows an end user to inject SQL queries into web forms.
THIS IS AN EXAMPLE OF SQL INJECTION:
$expected_data = 1;
$query = "SELECT * FROM users where id=$expected_data";
That will produce a regular query, while this code:
$spoiled_data = "1; DROP TABLE users;"
$query = "SELECT * FROM users where id=$spoiled_data";
will produce a malicious sequence:
SELECT * FROM users where id=1; DROP TABLE users;
It works because we are passing data directly into the query.
So, the data may alter the query and depending on the data passed, we will have either a regular output or the table `users` deleted.
While in the case of prepared statements we don't pass data directly into our query, and that's the point.
We are sending the query to the server first:
$db->prepare("SELECT * FROM users where id=?");
where the data is substituted by a variable called a "placeholder".
Note that the very same query is being sent to the server, but without any data in it! And then we're sending the data with the second request, totally separated from the query itself:
$db->execute($data);
We create the query, and then it is being sent without any user submitted data, instead the data is being replaced by a placeholder, the ?
Then we bind the parameters. We set the value of each paramenter like so:
s = string, i = integer in the order where they appear in both the query and the binding.