PDA

View Full Version : Save coding to database


SoireeExtreme
07-23-07, 04:52 AM
As the two posts below me, I'm saving a text area to the database but I'm saving it with coding such as html. the coding will save which is fine. But when I try to put the coding and text with some new lines just by hitting enter, it saves everything to the database but adds in the letters "rn" how can I get it to save with out those letters?

Its a bit annoying. But the only way I know to work around it right now is just to bunch everything together. Any help would be great. Thanks in advance.

My database "type" is "text"

If any info is needed please let me know.

fivefeet8
07-23-07, 12:31 PM
Where and in which way does it save it with the "RN"? Is it random? I'm thinking it might be something with magic_quotes, but not too sure.

evilghost
07-23-07, 12:45 PM
I'm thinking it's \r\n

Carriage Return + Newline, because the text area is multi-line.

str_replace(); should work.

SoireeExtreme
07-23-07, 07:27 PM
Thanks for the help, but I do not understand the str_replace();. I was reading the php manual but that didn't help. I forgot to tell you that I am still fairly new to php and mysql both. So please bare with me. But to give a bit more info.

rn is exactly how it looks when I hit the enter key for a new blank line just like typing a document.

I use print "Headline:<br><textarea name='headline' rows='1' cols='60'>$userstats3[headline]</textarea><br>"; as my text area

and I use if(isset($_POST['submit']))
{

$headline=$_POST['headline'];

$SQL = "update d_users set headline='$headline'where username='$userstats3[username]'";
mysql_query($SQL) or die("could not register");
echo "<center>Updated."; to save it to the database.


And when I have everything print out onto another page it comes out like this...


rn = new line done by enter key

words words words.rnrnwords words.

so in the process of saving it is converted someone from a new line to the rn. This helps. Please explain to me how to fix this problem. Because as I said the manual doesn't help me. I'm more of a hands on person. In the mean time I'll keep trying to figure this out and let you know if I do.

evilghost
07-23-07, 07:42 PM
Two things, and I'm not being critical, but rather trying to be helpful.

1) From a security perspective, never, never trust user input. In this case you need to be sure to escape the mysql input or you'll end up with a fatal SQL injection vulnerability. Use the function mysql_real_escape_string();

2) \r\n are injected from multi-line text-area, because you're writing it out in another page you need to convert \r\n to a HTML <br> tag or use the <pre> HTML entity to have it properly treat \r\n.

What I think you should do:

Change:
$headline=$_POST['headline'];

To:
$headline=mysql_real_escape_string(str_replace("\r\n","<BR>",$_POST['headline']));

Understand I'm not being critical one bit but with respect to #1 I can't pound that into your head enough ;) I'm always glad to help in any way I can, welcome to NVNews.

SoireeExtreme
07-23-07, 08:03 PM
I understand your being helpful. Reason why I came here is exactly for that. Help. Thanks for any help and tips you give and may give.

But... I changed my code as you suggested and I still end up with same problem as stated above.

I'm sure it'll hurt me in the long run. But instead of putting "\r\n" I took out the \\. When I did that it saved exactly as I wanted it to.

Is there another way to fix this? because as I said "\r\n" didn't work that way. And there are words out there that have rn together in them and this will affect those words only to create another problem for me. lol.

evilghost
07-23-07, 08:08 PM
You running PHP on Apache, on Linux or Win32?

\r\n seems to indicate Win32...

SoireeExtreme
07-23-07, 08:12 PM
I am using windows xp and yes php with apache. I have xampp.

evilghost
07-23-07, 08:15 PM
I honestly don't know why it's stripping \r\n and converting it to 'rn'.

It should properly escape \r\n and not treat it as 'rn' so I'm a little confused. It could be a PHP bug with respect to mysql interaction on Win32. See if you can Google and find an answer, I honestly don't know why it's happening.

Perhaps some other folks will chime in.

SoireeExtreme
07-23-07, 08:19 PM
Well thank you, I shall use google. But hey nothing is everything perfect. You did help me so I am better off then I was. If I find an answer I'll be sure to post it and let you know. Much appreciated. Thank you again. :D

evilghost
07-23-07, 08:23 PM
Glad to have been able to somewhat help... Stick around ;)

ViN86
07-25-07, 07:52 AM
remember that \ is the escape character in PHP, so when you want to use a character in a string like a quotation, you need to use \" - so maybe its escaping the \ character and displaying just "rn".

if this is the case, wouldnt it be "\\r\\n" ghost?

try...
$headline=mysql_real_escape_string(str_replace("\\r\\n","<BR>",$_POST['headline']));

evilghost
07-25-07, 07:59 AM
Never had to use \\r\\n, I would think since it's a CRLF you'd use \r\n to strip it...but you would be right, it could be a literal \r\n. I just confused myself I think.

ViN86
07-25-07, 08:17 AM
Never had to use \\r\\n, I would think since it's a CRLF you'd use \r\n to strip it...but you would be right, it could be a literal \r\n. I just confused myself I think.
well, the reason it's displaying "rn" on his screen is that its actually "\r\n" but PHP is escaping the backslashes and displaying the "rn" part. it's the same when you want to output a quote.

you cant use
echo """;
that would give you a syntax error...

youd have to use
echo "\"";

the backslash is not printed. so, although the "\r\n" is present, PHP is outputting it as "rn". so, to remove this you need to use "\\r\\n" as that would equate to "\r\n" in PHP.

this right? maybe im talkin out of my ass now :p

evilghost
07-25-07, 08:19 AM
AFAIK \r\n is chr(13) + chr(10), the text area has chr(13)chr(10) because of the Win32 CRLF and PHP sees it as \r\n just like a tab would be \t so to remove chr(13)chr(10) and convert it to <BR> you would use a str_replace on \r\n

ViN86
07-25-07, 08:26 AM
AFAIK \r\n is chr(13) + chr(10), the text area has chr(13)chr(10) because of the Win32 CRLF and PHP sees it as \r\n just like a tab would be \t so to remove chr(13)chr(10) and convert it to <BR> you would use a str_replace on \r\n
good point.

oh well, guess all we can do is wait for him to test it :)