Connect with Expertise | Find Experts, Get Answers, Share Insights

Insert and fetch value in MYSQL through textarea tag

 
Join Date: Oct 2007
Posts: 97
#1: Feb 9 '10
Dear All,

I am facing a problem when i display the data from mysql which i have inserted through textarea tag. I have a form where i copy and paste the details and submitted. But, when i fetch and display on the site, all the text are coming with any line break. its coming all in the same line which changes all the orginal paragraph format.

Kindly help me how to overcome the same and i can display the same format which i have entered in the textarea.

Thank you in advance.

With Regrds,
Robin

Atli's Avatar
E
M
C
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,618
#2: Feb 9 '10

re: Insert and fetch value in MYSQL through textarea tag


Hey.

Textareas take their data as plain-text, in which line breaks are represented as a combination of the "\r" and "\n" characters. - HTML, on the other hand, does not recognize the plain-text line breaks. It uses the <br> tag as line-breaks (<br /> in XHTML).

Which means that when you want to print plain-text data (such as the text from a textarea) into HTML, you need to convert the plain-text line-breaks to HTML line breaks.

Like with most common tasks, PHP has a function that does this for you: the nl2br() function. See the manual entry for more details.
 
Join Date: Oct 2007
Posts: 97
#3: Feb 9 '10

re: Insert and fetch value in MYSQL through textarea tag


Thanks for quick response and it working properly but i face a new problem if the content that i copy contain the ' ' (single quote), the mysql is facing issue and it wont allowing to insert the data. can you please help me in ignoring the single quote.

Thanks
Atli's Avatar
E
M
C
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,618
#4: Feb 9 '10

re: Insert and fetch value in MYSQL through textarea tag


That is a very common problem. In fact, that is the #1 security problem PHP applications face.
It's called SQL Injection; an attack on a database using purposefully malformed SQL statements.

To protect against it, you must escape all characters in user input that MySQL might mistakenly parse as is if it were something other than text. The two best methods of dealing with this in PHP/MySQL is to use the mysql_real_escape_string function or Parameterized Statements. - The latter is arguably superior, but the former easier to use.
 
Join Date: Oct 2007
Posts: 97
#5: Feb 9 '10

re: Insert and fetch value in MYSQL through textarea tag


Dear Atil,

Thank you very very much, now i have used mysql_real_escape_string function and its working properly. Thank you very very much for the help.


I will be back incase i face any more issue.

With Regards,

Robin
Reply