Page 1 of 1

saving paragraphs in multiline to mysql

Posted: Sun Nov 22, 2009 4:06 pm
by steven121
This might seem like a really silly question but how can i keep the format of posted text into a mysql database. e.g. i have a multiline textarea as part of a form. When i post this form to my php script I am saving all the data into the mysql database. However if the user makes carrage returns or copies text into the textarea that has paragraphs, I want to keep these paragraph format and saved into the database so i can echo out to a page and retain the format.

Re: saving paragraphs in multiline to mysql

Posted: Sun Nov 22, 2009 4:07 pm
by iankent
it should by default. a newline is represented by \n in a string, and that should be retained when you write it to the database.

if you output it as HTML then it'll look as though its been lost, as \n is ignored as a new line in HTML. you'd need to replace new lines with <br /> using something like str_replace. if you output it inside another textarea then you should see the newlines intact

hth

Re: saving paragraphs in multiline to mysql

Posted: Sun Nov 22, 2009 4:15 pm
by steven121
thanks works a treat i used the nl2br function

Re: saving paragraphs in multiline to mysql

Posted: Sun Nov 22, 2009 4:21 pm
by iankent
steven121 wrote:i used the nl2br function
for some reason I always forget that exists, don't know why... :banghead: