Friday, 16 August 2013

MySQLi insert string dies at & character

MySQLi insert string dies at & character

I have been fighting with a database insert problem most of the day now,
so now its time to seek help.
I have a comment form on my site which is submitted and processed via
ajax, 9 times out of 10 it will work flawlessly but I have noticed that
when you enter the & character, it ends the string prematurely but still
inserts the string to database before that character!
In my comments section, I allow snippets of code to be posted and
highlighted when displayed on page, so the & character is pretty much
needed for comparison purposes in any posted snippets.
I'm using a MySQLi class, the escape function is just a basic real_escape,
and mynl2br is just a custom nl2br function, this is the code that inserts
the comment data to database:
$name = $db->escape($_POST['name']);
$email = $db->escape($_POST['email']);
$url = $db->escape($_POST['web']);
$comment = $db->escape(mynl2br($_POST['comments']));
$script_id = $db->escape($_POST['script_id']);
$insertData = array(
'`author`' => ''.$name.'',
'`email`' => ''.$email.'',
'`url`' => ''.$url.'',
'`comment`' => ''.$comment.'',
'`script_id`' => ''.$script_id.'',
'`time`' => ''.time().''
);
if ($db->insert('script_comments', $insertData)) {
// sent success response back
exit;
}
can anyone understand why the & character is prematurely ending the
comment string?

No comments:

Post a Comment