Question

I am trying to protect against sql injections by using mysql_real_escape_string before inserting data to the database:

$data=mysql_real_escape_string($_POST['data']);

Now, the data is stored as such:

That\\\'s an apostrophe.\r\n\r\nThis new line isn\\\'t displaying properly!

So, I am trying to get it to display correctly inside of a textarea after pulling it back out of mysql:

$data = nl2br($data);

For whatever reason, this does NOTHING. I've even tried str_replace to replace the \r\n's with a <br>, but then the <br> just displays within the textarea.

How do I get what's in my mysql to display as:

That's an apostrophe.

This new line isn't displaying properly!
Was it helpful?

Solution 3

you probably have magic_quotes turned on, check it with
echo get_magic_quotes_gpc() or else you will double quote

"Sets the magic_quotes state for GPC (Get/Post/Cookie) operations. When magic_quotes are on, all ' (single-quote), " (double quote), \ (backslash) and NUL's are escaped with a backslash automatically. "

by the way, it's not a good ideia to use magic_quotes, try using one of this classes.

PDO http://br2.php.net/manual/en/book.pdo.php or mysqli http://br2.php.net/manual/en/book.mysqli.php

OTHER TIPS

The Best Solution..

$data= mysql_real_escape_string($_POST['data']); (You can insert it in your database if you want)

echo stripslashes(str_replace('\r\n',PHP_EOL,$data)); (The output is exactly as your input was)

Actually using mysql_real_escape_string doesn't fully protect you from SQL Injection attack.

The best way to do is to use PDO or MySQLi.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top