You can not use htmlentities when saving it. You must use htmlentities when displaying it. The rule of thumb is not to encode / misinform data until you need it. If you execute htmlentities on it while saving, you need to do html_entity_decode in the text when the user wants to edit the input. Therefore, you sanitize for what you need, and nothing more. When saving it, you need to perform a sanation for SQL injection, so you mysql_real_escape_string it. When displaying you need to sanitize XSS, so you htmlentities it.
Also, I'm not sure if you saw Darryl Hein's comment, but you really don't want magic_quotes to be included. They are bad, bad, something and are deprecated from PHP 5.3 and generally disappear in PHP 6.
Paolo bergantino
source share