title: PHP & mySQL: When exactly to use htmlentities?
body: <p><strong>PLATFORM:</strong>
  PHP &amp; mySQL</p>
  
  <p>For my experimentation purposes, I have tried out few of the XSS injections myself on my own website. Consider this situation where I have my form textarea input. As this is a textarea, I am able to enter text and all sorts of (English) characters. Here are my observations:</p>
  
  <p>A). If I apply only strip_tags and mysql_real_escape_string and do not use htmlentities on my input just before inserting the data into the database, <strong>the query is breaking</strong> and I am hit with an error that shows my table structure, due to the abnormal termination.</p>
  
  <p>B). If I am applying strip_tags, mysql_real_escape_string and htmlentities on my input just before inserting the data into the database, <strong>the query is NOT breaking</strong> and I am able to  successfully able to insert data from the textarea into my database.</p>
  
  <p>So I do understand that htmentities must be used at all costs but unsure when exactly it should be used. With the above in mind, I would like to know:</p>
  
  <ol>
  <li><p>When exactly htmlentities should be used? Should it be used just before inserting the data into DB or somehow get the data into DB and then apply htmlentities when I am trying to show the data from the DB?</p></li>
  <li><p>If I follow the method described in point B) above (which I believe is the most obvious and efficient solution in my case), do I still need to apply htmlentities when I am trying to show the data from the DB? If so, why? If not, why not? I ask this because it's really confusing for me after I have gone through the post at: <a href="http://shiflett.org/blog/2005/dec/google-xss-example" rel="noreferrer">http://shiflett.org/blog/2005/dec/google-xss-example</a></p></li>
  <li><p>Then there is this one more PHP function called: <strong>html_entity_decode</strong>. Can I use that to show my data from DB (after following my procedure as indicated in point B) as htmlentities was applied on my input? Which one should I prefer from: html_entity_decode and htmlentities and when?</p></li>
  </ol>
  
  <p><strong>PREVIEW PAGE:</strong></p>
  
  <p>I thought it might help to add some more specific details of a specific situation here. Consider that there is a 'Preview' page. Now when I submit the input from a textarea, the Preview page receives the input and shows it html and at the same time, a hidden input collects this input. When the submit button on the Preview button is hit, then the data from the hidden input is POST'ed to a new page and that page inserts the data contained in the hidden input, into the DB. If I do not apply htmlentities when the form is initially submitted (but apply only strip_tags and mysql_real_escape_string) and there's a malicious input in the textarea, the hidden input is broken and the last few characters of the hidden input visibly seen as  <code>" /&gt;</code> on the page, which is undesirable. So keeping this in mind, I need to do something to preserve the integrity of the hidden input properly on the Preview page and yet collect the data in the hidden input so that it does not break it. How do I go about this? Apologize for the delay in posting this info.</p>
  
  <p>Thank you in advance.</p>