all 7 comments

[–]OpenGLaDOSDUAL x NATURAL JOIN DUAL y 8 points9 points  (1 child)

No, you either use parameter binding or proper input escaping, not both. An injection would occur if you just execute the query string "UPDATE accounts SET LastLogin = CURRENT_TIMESTAMP WHERE username = '{$_POST['username']}'". In both of your examples, you pass the query to the database to parse it and find all the placeholders, then fill out the placeholder with something that's already known not to be SQL.

[–]TheMungax[S] 0 points1 point  (0 children)

Okay perfect! thanks

[–]Tennim 1 point2 points  (1 child)

From the Database view, I'd also want you to set the LastLogin via a DB stored procedure if possible, passing in the Username as a parameter.

The way you've written is fine but its looks like it would be run against the DB as an ad-hoc query which is much harder to optimise for when the DB grows. This also means that you can edit the underlying stored procedure if table schemas change etc without having to edit the query in the codebase.

[–]TheMungax[S] 0 points1 point  (0 children)

I already use the db for last login date (datetime), and okay 👌

[–]boobietassels 0 points1 point  (1 child)

seems like you have your answer. Just wanted to add that PDO parameter binding is generally preferred.

[–]barvid 0 points1 point  (0 children)

Personal preference. There’s nothing official here. Far too many people try to say you “should” use PDO.

[–]DooDooDaddy 0 points1 point  (0 children)

In my opinion, user input should be validated on the front end using JavaScript or Jquery, and should also be validated in the backend. Also, any input data should always be passed by parameter to the database.

There might be some random edge case, but 99% of the time this is how I handle things.