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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.