The misconception is that you want to avoid typing, which is wrong. You must filter the output (and the database is also the result).
This means that when you mysql_real_escape_string() form, you use mysql_real_escape_string() to send (output) data to the database, and you use htmlspecialchars() to display the contents on the screen. The same principle applies to regular expressions in which you would use preg_quote() , etc.
No matter where the data comes from, you should avoid it in the context of where you send it.
So, to prevent XSS attacks, you should use htmlspecialchars() / htmlentities() . mysql_real_escape_string has nothing to do with XSS (but you should still use it when sending data to the database).
source share