MySQL slashes and nl2br

ali47007

New Member
I am trying to store HTML posted from a textarea into a database. I have a textarea inside a form which I have called "message". The PHP code that processes it is:\[code\]if(isset($_POST['submit'])){ if(isset($_POST['title']) && isset($_POST['message'])){ $title = $_POST['title']; $message = $_POST['message']; if(get_magic_quotes_gpc()){ $title = stripslashes($title); $message = stripslashes($message); } $title = mysql_real_escape_string($title); $message = mysql_real_escape_string($message); $q = "INSERT INTO table (title,datetime,text) VALUES ('{$title}',NOW(),'{$message}')"; $rows_affected = $db->exec($q); if($rows_affected > 0){ echo "<p>Done.</p>"; } else { echo "<p>Failed. </p>"; } }}\[/code\]The problem I am having is then retrieving this and converting newlines to \[code\]<br />\[/code\]. Here is what I am doing:\[code\]$res = array();$order = array("\r\n","\n","\r");$replace = '<br />';$q = "SELECT title,datetime,text FROM table";$res = $db->get_all($q);if($res){ foreach($res as $result){ $result['title'] = stripslashes($result['title']); $result['text'] = str_replace($order, $replace, stripslashes($result['text'])); } }echo "<pre>";print_r($res);echo "</pre>";\[/code\]I just can't get rid of those pesky \[code\]\r\n\[/code\]'s in the message. I have tried changing \[code\]$order\[/code\] to\[code\]$order = array("\\r\\n","\\n","\\r");// and even$order = array("\\\r\\\n","\\\n","\\\r");\[/code\]but nothing seems to work. Any ideas?
 
Back
Top