No mysql_real_escape_string()
! You should use PDO. It uses prepared statements, which will not be vulnerable to injection attacks because MySQL is given the unparameterized SQL first and then given the data to plug in.
For example:
$dbh = new PDO();
$stmt = $dbh->prepare('INSERT INTO data (something) VALUE(:userInput)');
// No mysql_real_escape_string necessary
$stmt->execute(array(
':userInput' => $_POST['userInput']
));
htmlspecialchars()
shouldn't be used on all input, but it should be used! Although typically applied after data is retrieved from the db (although, it might be a good idea to do it before in case it is forgotten afterward), it is useful for user input that you will be echoing into your HTML pages. It protects you against XSS (Cross Site Scripting) attacks, in which a malicious user can add <script>
tags that contain malicious code into an input field on your site. When other users visit the page on which this malicious user posted, their browser will interpret the evil scripting, which could do things such as steal session ids or attempt CSRF (Cross Site Request Forgery).
Bottom line: You should use it before echoing any user content to your pages. Unless that content has been validated by a rigorous filter (like one for birthdates which only accepts mm/dd/yy). If you're unsure, then use it anyways. It won't hurt. It will only help!
Further Reading: