Solved

Preserving special characters and carriage returns from textarea into database

Posted on 2007-11-20
2
1,527 Views
Last Modified: 2013-12-13
I have a form with a textarea and I want to insert the textarea value into a table...here is the table structure and code:

messageid INT
type TINYINT
authorid INT
datetime DATETIME
subject VARCHAR 100
message MEDIUMTEXT

And here is the code:
$sql = "INSERT INTO messages ";
$sql .= "(";
$sql .= "type, ";
$sql .= "authorid, ";
$sql .= "datetime, ";
$sql .= "subject, ";
$sql .= "message";
$sql .= ") VALUES (";
$sql .= "0, ";
$sql .= $fromuserid . ", ";
$sql .= "\"" . date("Y-m-d H:i:s", time()) . "\", ";
$sql .= "\"" . $subject . "\", ";
$sql .= "\"" . addslashes($message) . "\"";
$sql .= ")";
$safesql = & new SafeSQL_MySQL; //class module to protect against SQL injection attacks
$sql = $safesql->query($sql);
mysql_select_db($mysql);
mysql_query($sql);      

This inserts this text fine:
"

------------ Original Message -----------
From: <a href="profile.php?id=71">Meggie D</a>
Date: 2007-11-19 10:58 AM

Testing 1..2..3...
"

However, upon selecting and displaying this data, it seems as if somewhere in the process all the line feeds and carriage returns got stripped or do not come back out of the table correctly.

Any ideas as to what it is I'm doing wrong?

Thanks.
0
Comment
Question by:HarpuaFSB
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20323193
you have to be aware that the newline, in html, will do nothing.
you have, for the linebreak in html to get "visible", be translated into the string <br>.

check out the nl2br() function of php for that

0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20323227
as angellll said, it is because the newline "\n" character won't be displayed in html, so you will need to do this when you insert and it will convert the newline characters into <br>'s:


$sql = "INSERT INTO messages ";

$sql .= "(";

$sql .= "type, ";

$sql .= "authorid, ";

$sql .= "datetime, ";

$sql .= "subject, ";

$sql .= "message";

$sql .= ") VALUES (";

$sql .= "0, ";

$sql .= $fromuserid . ", ";

$sql .= "\"" . date("Y-m-d H:i:s", time()) . "\", ";

$sql .= "\"" . $subject . "\", ";

$sql .= "\"" . nl2br(addslashes($message)) . "\"";

$sql .= ")";

$safesql = & new SafeSQL_MySQL; //class module to protect against SQL injection attacks

$sql = $safesql->query($sql);

mysql_select_db($mysql);

mysql_query($sql);  

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now