0

Is mysql_real_escape_string() with sprintf needed only at login page or at every mysql_query after login, for preventing SQL injection?

1

4 Answers 4

6

You should use mysql_real_escape_string() every time you insert user-posted data into a query, or use a database wrapper like PDO that can do prepared statements. That would be better, because they do the job of sanitizing for you.

If you are working on the overall security of your site, this is great and definitely necessary. If you are looking for reasons why your site was hacked, though, I doubt this was done through a SQL injection, as your actual HTML code was affected (or so I thought, I may be wrong). This would be only possible if you had your FTP password stored somewhere in the database.

Sign up to request clarification or add additional context in comments.

Comments

5

You should use mysql_real_escape_string for any user supplied data that is going to be ran through an SQL query.

2 Comments

More likely, you shouldn't be using mysql_* at all.
This is true as of today, not so much two years ago. :D
3

Use it when you do not trust the input. And never trust a user input.

Comments

-1

In any instance that you accept user input, you should use mysqli_real_escape_string on it before sending it to the database. It is a good idea to use trim() on the input as well.

1 Comment

I take it the -1 was for recommending mysql_real_escape_string. Fixed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.