The main workflow should be
$data = $_POST['somefield which will go into the database']; ... do data validation ... if (everything ok) { $escaped_data = escape_function($data); $sql = " ... query here with $escaped_data ... "; do_query($sql); }
In principle, data that has been escaped to insert the database should ONLY be used to insert the database. It makes no sense to pre-process everything and rewrite all the data using values โโshielded with db, when only 2 or 3 of the 50 (say) values โโactually go anywhere near db.
The same goes for htmlspecialchars. Do not send data through htmlspecialchars if it is not suitable for displaying an HTML type.
Do not store data in a database formatted for a specific purpose, because if you ever need information in a different form for any other purpose, you need to cancel the escaping. Always store raw / unformatted data in db. And note: escaping is done using mysql_real_escape_string (), and the company is not actually stored in db. This is there just to make sure that the data gets into the database SAFE. What is actually stored in db is raw unsecured / unquoted data. After that, it is โsafeโ in the database.
eg. consider screening functions as handcuffs on a transferred prisoner. While the prisoner is inside the prison, cuffs are not needed.
source share