PHP problems modifying data
<p>Having problems writing data to my database. When I edit a page and insert data in with HTML tags it gets corrupted. e.g. some text enclosed by &lt; h2 > tags and when I check the database I see extra characters &lt;h2&amp;gt....</p>

<p>&lt;h2&gt;About Page&lt;/h2&gt;<br />
<br />
&lt;p&gt;This is the about page&lt;/p&gt;    </p>

<p>Then when I reload my webpage I see the text I typed and the tags, which obviously I don't want visible. Any ideas why this is happening?  </p>

<pre><code>&lt;?php 
session_start();
include_once "admin_check.php";
?&gt;
&lt;?php 
first thing, I did not do that
$pid = preg_replace('#[^0-9]#', '', $_POST['pid']); // filter everything but numbers     for security

include_once "../scripts/connect_to_mysql.php";
$sqlCommand = "SELECT pagetitle, linklabel, pageorder, pagebody FROM pages WHERE id='$pid' LIMIT 1"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $pagetitle = $row["pagetitle"];
    $linklabel = $row["linklabel"];
    $pageorder = $row["pageorder"];
    $pagebody = $row["pagebody"];
    $pagebody = str_replace("&lt;br /&gt;", "", $pagebody);
    $pagebody = nl2br(htmlspecialchars($pagebody));
} 
mysqli_free_result($query); 
?&gt;

&lt;?php
$pid = $_POST['pid'];
$pagetitle = $_POST['pagetitle'];
$linklabel = $_POST['linklabel'];
$pagebody = $_POST['pagebody'];
// Filter Function -------------------------------------------------------------------
function filterFunction ($var) { 
    $var = nl2br(htmlspecialchars($var));
    $var = str_replace("'", "&amp;#39;", $var);
    $var = str_replace("`", "&amp;#39;", $var);     
    return $var; 
} 
$pagetitle = filterFunction($pagetitle);
$linklabel = filterFunction($linklabel);
$pagebody = filterFunction($pagebody);
// End Filter Function --------------------------------------------------------------
include_once "../scripts/connect_to_mysql.php";
// Add the updated info into the database table
$query = mysqli_query($myConnection, "UPDATE pages SET pagetitle='$pagetitle', linklabel='$linklabel', pagebody='$pagebody', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));

echo 'Operation Completed Successfully! &lt;br /&gt;&lt;br /&gt;&lt;a href="index.php"&gt;Click Here&lt;/a&gt;';
exit();
?&gt;
</code></pre>
