/    Sign up×
Community /Pin to ProfileBookmark

insert and retrieve "NEW LINE" into/from MSSQL

Hi,

I have a form with a <textarea> field for people to put in their comment. People can type in “new line” to the textarea field by just pressing the “enter” button on the keyboard. I want to know how I can store the comment WITH the “new line” information to the sql server by a insert statement like this:

INSERT INTO Comment (id, comment) VALUES(NEWID(), $_POST[‘comment’])

Will the above statement store the “new line” information into the database? What character exactly is stored in the MSSQL database for a “new line”? I checked lots of places online and it seems that MSSQL doesn’t recognize ‘rn’ as new line.

Also, when I retrieve some information containing “new line” from the database by a select statement, how do I get the “new line” printed as a real new line on the webpage? Because echo ‘rn’ won’t result in a new line on the webpage, only “<br>” does.

I’m not asking for everything without doing my own thinking. All I need is a tip and I can figure the remaining answers out later by myself. Thank you everyone for any advice or tips.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiFeb 05.2009 — The answer is, yes it does save the newline character. The exact character will depend on the OS used, but it shouldnt matter because you can use the nl2br() function to replace newlines with <br /> tags automatically.

Also as a side note, I'm not sure if the query you posted was just for clarity, but directly including user input without cleaning it is very dangerous, so make sure you're doing that.
Copy linkTweet thisAlerts:
@JiabaoauthorFeb 05.2009 — Thank you very much. I've also found the nl2br function that solves my problem.

For cleaning part, I developed a escapeSingelQuote() to do the cleaning, I'm not sure if that's enough and anything else is needed, assuming that my users are not going to put HTML code intentionally to screw up with the display.
Copy linkTweet thisAlerts:
@MindzaiFeb 05.2009 — ...assuming that my users are not going to put HTML code intentionally to screw up with the display.[/QUOTE]

Dont make any assumptions when it comes to user input! Never trust your users!
Copy linkTweet thisAlerts:
@SameerMirzaJun 19.2009 — Hi all,

I am dealing with the same issue right now. but I have no idea how would I detect the <Enter> key stroke in user input and store it in the databse in order to retrieve it later to display it in the format it was enterd.

Can any one please help me out with it most probebly the op ?. As you have already found a solution.

Regards,

Sameer.
Copy linkTweet thisAlerts:
@MindzaiJun 25.2009 — The answer is already posted in this thread...

The answer is, yes it does save the newline character. The exact character will depend on the OS used, but it shouldnt matter because you can use the nl2br() function to replace newlines with <br /> tags automatically.[/quote]

You don't need to detect the enter keystroke. Pressing enter inserts a newline character. Although you can't see it, it is there. The data is stored in the database exactly as it appears in the form input, new lines included.
Copy linkTweet thisAlerts:
@SameerMirzaJun 25.2009 — Thanks for the reply. But I did pasted the question because I wasn't getting the data as as entered by the user. But I should have provided the complete details. Actually I am using ajax/jscript to pass the data to .php where I store it in db. I also allow user to update and the data and thats where the problem comes. Jscript dosn't pass the value as it is to the php variable.

well i have figure out the solution. which to use the escape() function before passing the string to .php page. then I was getting antoher problem which is that it was repeating the slashes (i think some security reason, not sure) but that can be overcomed using php stripslashes() function.


rgds,

Sameer.
×

Success!

Help @Jiabao spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...