nV News Forums

 
 

nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   Save coding to database (http://www.nvnews.net/vbulletin/showthread.php?t=95366)

SoireeExtreme 07-23-07 04:52 AM

Save coding to database
 
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

Re: Save coding to database
 
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

Re: Save coding to database
 
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

Re: Save coding to database
 
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
PHP Code:

     print "Headline:<br><textarea name='headline' rows='1' cols='60'>$userstats3[headline]</textarea><br>";

as my text area

and I use
PHP Code:

    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

Re: Save coding to database
 
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:
Code:

$headline=$_POST['headline'];
To:
Code:

$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

Re: Save coding to database
 
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

Re: Save coding to database
 
You running PHP on Apache, on Linux or Win32?

\r\n seems to indicate Win32...

SoireeExtreme 07-23-07 08:12 PM

Re: Save coding to database
 
I am using windows xp and yes php with apache. I have xampp.

evilghost 07-23-07 08:15 PM

Re: Save coding to database
 
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

Re: Save coding to database
 
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

Re: Save coding to database
 
Glad to have been able to somewhat help... Stick around ;)

ViN86 07-25-07 07:52 AM

Re: Save coding to database
 
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...
Code:

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


All times are GMT -5. The time now is 11:44 PM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.