PHP santization of textarea input My application will accept textarea content that is submitted by a user, and i would like some people to review my code to make sure there is no security vulnerability such as XSS. 

My mySQL column that will save this information is a column of type TEXT and is not required and nullable.

When storing the data to database, my script is doing the following:

// to avoid inserting html tags in the database
$input = str_replace(["&lt;", "&gt;"],"", $_POST['userinput'])

// to avoid saving problematic characters such as quotes 
$cleanInput = htmlentities(input , ENT_QUOTES)

// store the content
$addPostStmt = $conn -&gt; prepare("
  INSERT INTO posts(description) VALUES ( ?)
");

$addPostStmt -&gt; bind_param("s", $cleanInput); 
$addPostStmtExecute = $addPostStmt -&gt; execute();


When presenting the data to the user, the script is doing the following:

&lt;?php echo htmlspecialchars(html_entity_decode($post['description']), ENT_SUBSTITUTE); ?&gt;
No, don't do that. You seem to be filtering and escaping values out of paranoia rather than understanding what exactly would lead to a vulnerability. As a result, you are corrupting your data.

A well designed application should use the database to store the value that the user typed into the textarea, not some mangled representation of it. If you mangle the data like that before storing it, then:


Certain characters that the user typed get dropped. (What if the user input is x + 3 &lt; 5? The data would no longer make sense after you drop the &lt; character.)
Your database is not reliably searchable. (What if the user input is She said "yes!"? Then you would store a value in the database with &amp;quot; in it.)
If you arbitrarily apply escaping to string just in case, then you'll have a hard time keeping track of how to unescape it correctly when regurgitating the data. (This often leads to bugs where the user sees garbage like his &amp;amp; hers, or even worse, his &amp;amp;amp; hers.)


What's the right way? Don't mangle the data; just store it faithfully:

// store the content
$addPostStmt = $conn -&gt; prepare("
  INSERT INTO posts(description) VALUES (?)
");

$addPostStmt -&gt; bind_param("s", $_POST['userinput']); 
$addPostStmtExecute = $addPostStmt -&gt; execute();


When outputting the data as HTML, apply HTML escaping:

&lt;th&gt;Description:&lt;/th&gt;&lt;td&gt;&lt;?php echo htmlspecialchars($description); ?&gt;&lt;/td&gt;