line break in mysql

  harristweed 11:21 07 Mar 05
Locked

I store text in a mysql database via a form with a text box.

Any carriage returns are lost when the text is later retrieved and displayed on a web page. So i lose formating and the text is not easily read. Yet when I retrieve and display the text in a form text box the formating and line breaks are in the correct places again.

Has anyone got any ideas how I can retain the formatting (carriage returns) in the web page?

I know I can get around this by placing b r / > in the text but as I want other users to input text, this is not ideal.

  LeadingMNMs 18:14 07 Mar 05

The problem here is that HTML ignores white space, and hence you lose all newlines. Although I have never done this, I think you'll have to go through the input / output and replace all newline characters (ASCII 10, or the character '\n') with a <br /> tag.

It may help if you can let us know what 'language' you are using to handle the form input and retrieval of information.

  harristweed 19:13 07 Mar 05

Thanks for taking the time to help.

I'm using PHP to insert data into a mysql database.

The text box posts to itself to insert in to the database.

"< form enctype="multipart/form-data" <?php echo " ACTION=\"$_SERVER[PHP_SELF]\" METHOD=POST"; ?>>" (i'm uploading photo's in the same form.

You can see the results here (not finished yet)

click here

Is there a php function that can retain the carridge returns, similar to addslashes?

I can go through and add html breaks manually, but there must be a better way

  Matt45 20:04 07 Mar 05

There certainly is a PHP function to do this. I assume that you insert query will be similar to:

INSERT INTO tblname VALUES '$field1','$field2','$field3'

In order to retain the lines breaks and replace new lines automatically with <br> values you just need to add nl2br($fieldname) where fieldname is the name of the field that has the multiple lines in so it would look something like this.

INSERT INTO tblname VALUES '$field1','$field2',nl2br($field3)

Hope that makes sense

  harristweed 08:49 08 Mar 05

Many thanks Matt

INSERT INTO tblname VALUES '$field1','$field2',nl2br($field3)

did not work!

However:

$field3=nl2br($field3);

INSERT INTO tblname VALUES '$field1','$field2','$field3'

did the job.

This thread is now locked and can not be replied to.

AMD Ryzen release date, specifications and features: Three CPUs from the Ryzen 7 range now…

1995-2015: How technology has changed the world in 20 years

Glassworks Barcelona shows how it captured the artistic imagination of Conor in A Monster Calls

Best iPhone games 2017 | Best iPad games 2017: 162 fantastic iOS games that you need to play right…