I'm having troubles with saving certain characters to MySql database table from PHP. I have a WYSIWYG editor through which I create page content and save them to a DB. When I have ' ' or apostrophe in my code, the html text doesn't get saved to the DB. Following is my code:
sending data over to the server:
function SaveContent()
{
var content = CKEDITOR.instances.ContentEditor.getData();
content = content.replace(/"/g, "'");
var page = document.getElementById("hfpage").value;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
location.href=document.referrer;
}
}
//alert(content);
xmlhttp.open("GET","savecontent.php?content="+content+"&page="+page,true);
xmlhttp.send();
}
php code for database saving:
//save content editted/added through the HTML editor
function SaveContent($page, $content, $user)
{
$con = $this->OpenConnection();
//first check if the content exists, if so do an update
$sql = "SELECT * FROM sitecontent WHERE Page = '" . $page . "'";
$content = mysql_real_escape_string($content);
$rowcount = mysqli_num_rows(mysqli_query($con,$sql));
if( $rowcount > 0 )
{
//run update
$sql = 'UPDATE `sitecontent` SET `Content`="' . $content . '", `UpdatedBy`="' . $user . '", `LastUpdated`= NOW() WHERE `Page` = "' . $page . '"';
$result = mysqli_query($con,$sql);
//echo $sql;
}
else
{
//run insert
$sql = 'INSERT INTO `sitecontent` (`Page`, `Content`, `LastUpdated`, `UpdatedBy`) VALUES ("' . $page . '","' . $content . '", NOW(),"'. $user . '")';
$result = mysqli_query($con,$sql);
}
}
Notice that I already use 'mysql_real_escape_string' as I already saw on some posts on forums, but this doesn't help.
Appreciate if someone could help me with the problem.