Jump to content

Problem with linebreaks when using textarea and MySql


Zombay

Recommended Posts

Hello! I run into this simple problem and couldn't find any solutions from web, so I came here to ask you guys :)

 

I don't understand what happens to linebreaks when I post textarea and save value to Mysql...

 

Btw, I notided that linebreaks works properly if I don't use mysqli_real_escape_string, but I like to keep script as secure as possible...

 

If I input this in to textarea:

row1

row2

 

row4

 

row6

 

 

I get this in textarea and html-code:

1\r\n2\r\n3\r\n4\r\n5

 

 

This is how I try to do it:

<?

//>>> Connect to Mysql

// Secure $_POST -data
$_POST['mytext'] = mysqli_real_escape_string($db, $_POST['mytext']);


// Insert to Mysql
mysqli_query($db, "INSERT INTO mysql_table (mytext) VALUES ('$_POST[mytext]')");

// Get data from Mysql
$query = mysqli_query($db, "SELECT mytext FROM mysql_table';");
$row = mysqli_fetch_array($query);
mysqli_free_result($query);

?>

<textarea name="mytext"><? print $row['mytext']; ?></textarea>

<?

// nl2br function
$row['mytext'] = nl2br($row['mytext']);

// print text
echo $row['mytext']; 

?>

 

Aaaarrggh! Auts, man! You got it! I was wondering there is something weird about this.. There was foreach $_POST values with mysqli_real_escape_string() in the beginning of code, so i used mysqli_real_escape_string() twice.... I'm terrible sorry I wasted your time!

 

Btw, do you think it's secure enought if I just add htmlspecialchars() before printing?

$row['mytext'] = htmlspecialchars($row['mytext'], ENT_QUOTES);

 

 

Ya know, it seems to be browser-dependent. I was just testing it out, and in Safari it just spits the raw markup back into the textarea like I'm used to. In FF, the closing </textarea> tag is truncated, so it seems something I though worked across the board actually doesn't.

Archived

This topic is now archived and is 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.