Dear All,
I am trying to pull data from the database using PDO, is the code below safe from an attack or do I need to bind anything? If I need to bind anything, please help me write the correct code because all the videos that I bought on e-commerce used mysqli and it is the procedural way without validating anything in the name of keeping things simple.
Please, help me with the code that will work instead of just suggesting what to do, it will help me write the code faster and also make it more clearer.
Thanks for the link, I know how to insert data with prepare statement in PDO but what I don’t rely know is how to pull data from database using prepare statement, if anyone can help with the code above it will make it more clearer to me and and time safer, then I will continue from there in my project.
The data from $_GET can be anything at all the user wants to put in it. It’s just a case of them writing the URL with a harmful value to the p_cat query string.
www.example.com?p_cat=some-nasty-sql-injection
And you submit that query to your database…
It’s safer to use a prefpared statement:-
$p_cat_id = intval($_GET['p_cat']); // ensures the value in a integer
$get_p_cat = "SELECT * FROM product_categories WHERE p_cat_id = ?"; // Use a ? placeholder for the variable value
$run_p_cat = $connection->prepare($get_p_cat); // Sends the statement to the database
$run_p_cat->execute([$p_cat_id]); // Execute the query, passing in the value
$row_p_cat = $run_p_cat->fetch(); // Fetch the data
How does that help?
An injection attack can work by modifying the statement you make, possibly by ending it, then starting a whole new statement, all within the value passed in.
But when you prepare a statement, the DB knows what the whole structure of the query will look like from start to finish, excluding the user submitted values. So it’s all there hard-coded, free of tampering. In this case it’s a single select query. There is no second query that drops a table or fetches passwords from the user table, it just does what you wanted it to and nothing more.
Only after then, you pass in the user data. Because the structure of the whole statement is already defined, that user data will only ever be considered as a value to search for, it can’t be a termination of the initial query, followed by a second spurious query, because that’s not how the pre-defined, hard-coded, untampered statement was.
I have read the forwarded tutorials and they really helped me, but I have another question about LIMIT Clause, is posted under my reply to SamA74 below, help check if I am right.
As these values are hard coded by yourself, there is no need to use placeholders, or to prepare and execute. You may simply put the value directly into the statement.
This depends on what data is in the table product_categories.
Is it possible for the user to modify data in this table or insert new data?
Do you validate the data which is stored into this table?
If not, you need to use htmlentities() to make the string secure
The risk is not the reading from database but the creating of html from the database content. It’s very easy to put an XSS attack in a database string if you do not take care of that
Sorry folks but the LIMIT advice which involves directly adding values into the sql string is very very very bad. The whole point behind prepared statements is to avoid the need for the programmer to take specific action based on data types. If you are going to the trouble of using prepared statements then all variables should be bound regardless of source or types.
In the original post the bindParam should have been used instead of bindValue.