Jump to content

mysql_real_escape_string() but allow \r\n?


shortysbest

Recommended Posts

i have a textarea and when you type,

"this is line 1

this is line 2

this is line 3"

 

when i print the POST data it prints "this is line 1\r\nthis is line 2\r\nthis is line 3"

 

my code is:

 

nl2br(strip_tags(mysql_real_escape_string($_POST['status'])))

 

i have nl2br() to add the line breaks, but mysql_real_escape_string eliminates the function

 

when i put it like this tho:

mysql_real_escape_string(nl2br(strip_tags($_POST['status'])))

 

the same input as above comes to:

this is line 1

\r\nthis is line 2

\r\nthis is line 3

 

but it still shows \r\n

Link to comment
Share on other sites

<?php
$status = mysql_real_escape_string(nl2br(strip_tags($_POST['status']))); 
$date = date('Y-m-d h:m:s e');
if($_POST['share']) {if($status){
mysql_query("INSERT INTO status VALUES('','$status','$session','$date')");
print "<script>self.location='index.php?node=profile&user=".$uid."'</script>"; 
}}
?>
<form action='index.php?node=profile&user=<?php print $uid?>' method='post'>
<textarea style="width:532px;" class="inputbox" name="status" rows="2" wrap="physical"></textarea>
<input style="float:right;margin-top:5px;" class='button' name='share' type='submit' value='Share'>
</form> 
</div>
<?php $select_status = mysql_query("SELECT * FROM status WHERE user='$uid' ORDER BY id DESC ");
$query = mysql_query("SELECT * FROM users WHERE id='$uid'");
$ua = mysql_fetch_assoc($query);
while ($status = mysql_fetch_array($select_status))
{
?>

Link to comment
Share on other sites

You don't need to use mysql_real_escape_string to echo the data, but you do use it before using it in a database query string. If all you're doing is echoing $_POST data, you can do either of these.

 

echo "<pre>" . $_POST['status'] . "</pre><br />";
echo nl2br($_POST['status']);

Link to comment
Share on other sites

You don't need to use mysql_real_escape_string to echo the data, but you do use it before using it in a database query string. If all you're doing is echoing $_POST data, you can do either of these.

 

echo "<pre>" . $_POST['status'] . "</pre><br />";
echo nl2br($_POST['status']);

 

the date that it is around does go to the database, i was just echoing it out for testing purposes.

Link to comment
Share on other sites

Change this:

<?php
$status = mysql_real_escape_string(nl2br(strip_tags($_POST['status']))); 
?>

to

<?php
$status = (get_magic_quotes_gpc())?mysql_real_escape_string(stripslashes($_POST['status'])):mysql_real_escape_string($_POST['status']);
?>

 

When you want to display this back to the user after pulling it from the database, do something like this (this assumes that the retrieved value is in $rw['status']:

<?php
$status = (get_magic_quotes_gpc()):nl2br(strip_tags(stripslashes($rw['status']))):nl2br(strip_tags($rw['status']));
echo $status;
?>

 

Ken

Link to comment
Share on other sites

The I don't see what the problem is. Do you just not want the \r\n going into the database record? What is the goal here?

 

I want the \r\n to go to the database, but when it prints out i don't want it showing, i want it to be hidden and add the line spaces.

 

 

Link to comment
Share on other sites

  • 2 years later...

i'm encountering the same problem about nl2br and mysql_real_escape_string problem, you said that you had solved that problem, i'd love to know how you solved it because i have no clue, thank you..

 

Ben

 

Best regards

 

email: at2ot_m@hotmail.com

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.