Jump to content

PHP line breaks


c_pattle

Recommended Posts

I'm trying to insert the contents of a textarea into a MySQL database but I am wondering what the best way is to preserve the users line breaks.  I know that you can use nl2br() to convert "\n" into "<br />" when the retrieve data from the database but I am looking at a way to insert these line breaks when I enter the data. 

 

Thanks for any help. 

Link to comment
https://forums.phpfreaks.com/topic/223252-php-line-breaks/
Share on other sites

If it's a simple textarea that the user is filling in (with no HTML), then you don't need to process the new lines. Just process them when they are retrieved from the db to display on the page, use nl2br() and htmlentites().

 

Don't forget when inserting the users data use strip_tags() to avoid XSS and your preferred real_escape() function.

Link to comment
https://forums.phpfreaks.com/topic/223252-php-line-breaks/#findComment-1154159
Share on other sites

I suspect you are echoing the data out onto the page and the data is appearing with no new lines. That is normal because web browsers ignore the new line character, the browser will only show a new line if there is a <br /> or <p> or another block level element. If this is the case, view the source of the page from the browser and you will see the new lines.

Link to comment
https://forums.phpfreaks.com/topic/223252-php-line-breaks/#findComment-1154557
Share on other sites

I am not sure what I am doing wrong but the line breaks aren't being preserved when I submit it to the database.  When I review the database there are no line breaks and when I look at the page source there are no "\n".  Below I have given an example of how I am submitting data. 

 

sprintf("insert into table (name, content) values (\"%s\", \"%s\")", mysql_real_escape_string($_GET['name']), mysql_real_escape_string($_GET['content']));

 

 

Link to comment
https://forums.phpfreaks.com/topic/223252-php-line-breaks/#findComment-1154778
Share on other sites

I've taken out the sections of code that are being used. 

 

Here is the form code

 

<label for="text">Your text <span id="text_status"></span></label>
<textarea name="text" id="text" class="validate"></textarea>

 

and this is PHP code used to submit it

 

$submit_sql = sprintf("insert into table (name, text, category) values (\"%s\", \"%s\", \"%s\")", mysql_real_escape_string($_GET['name']), mysql_real_escape_string($_GET['text']), mysql_real_escape_string($_GET['category']));

$submit_rs = mysql_query ($submit_sql, $mysql_conn);

 

Could it be perhaps that I am using GET rather than POST?

Link to comment
https://forums.phpfreaks.com/topic/223252-php-line-breaks/#findComment-1155473
Share on other sites

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.