Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 30th, 2005, 1:24 PM   #1
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 172
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
sql queries with ' \n \r

Alright I have this problem and continue to have it. Everyone knows about Live Journal, myspace. This works with it too. All of them have long imput boxes. How have you guys solved the problem of dealing with putting in the slashes (\) for the n, r, and '? I got the ' part I just need help with the \n \r.

More Detail: I have a user inputted string of
"I'll have you when....[return]
when i'll you[return]
[return]
i'd eat you up [return]"

I need to get that to "I\'ll have you when....\rwhen i'll you\r\ri'd eat you up \r"

Any Ideas? I currently have addslashes() which works for the ' but not r and n (WHICH I NEED), could it be the way i'm having the form using it? I have the input box as this: "
<textarea name="journal" cols="50" rows="10">' . $_POST['journal'] . '</textarea>
PhilBon is offline   Reply With Quote
Old Dec 30th, 2005, 2:36 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Try this:

[PHP]<?php
$str = "I'll have you when....\nwhen i'll you\n\ni'd eat you up";
$str = addslashes ($str);
$str = str_replace("\n", '\n', $str);
$str = str_replace("\r", '\r', $str);
echo $str;
?>[/PHP]
titaniumdecoy is offline   Reply With Quote
Old Dec 30th, 2005, 3:18 PM   #3
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 172
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
The Problem with that is where does it get the \n and \r first? It's all user inputted and so I don't want my user to have to do that every time.
PhilBon is offline   Reply With Quote
Old Dec 30th, 2005, 4:34 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
When the user types:

"I'll have you when....
when i'll you

i'd eat you up"

This string will be represented in PHP as:

"I'll have you when....\nwhen i'll you\n\ni'd eat you up"

In PHP \n is not text but a newline character.

So if the user types the text above including returns, your PHP program will see it ($str) as having \n newline characters, which the code I posted above will convert into the string "\n".

It might help to understand that in double quotes, "\n" is a newline character, and "\\n" is the string \n. In single quotes, '\n' is the string \n.
titaniumdecoy is offline   Reply With Quote
Old Dec 30th, 2005, 5:08 PM   #5
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 172
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
this is how the form takes it in and puts it into the db
Inputted from textarea:
I'll have you when....
when i'll you

i'd eat you up
end textarea
$journal = $_POST['journal'];
$journal = addslashes($journal);
echo $journal;
require_once('../inl/connect.php');
$query = "Insert into `journal` (`date`, `time`, `title`, `journal`, `mood`, `who`) Values ('" . $date . "', '" . $time . "', '" . $title . "', '" . $journal . "', '" . $mood . "', '" . $who . "')";
$result = @mysql_query($query) or die('Query failed: ' . mysql_error());
mysql_close();

That's how it's done so I don't know where the mix up coulde happen. When I echo $journal it just prints it as one line without the \n and \r
PhilBon is offline   Reply With Quote
Old Dec 30th, 2005, 6:10 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
[PHP]$journal = $_POST['journal'];
$journal = addslashes($journal);
$journal = str_replace("\n", '\n', $journal);
$journal = str_replace("\r", '\r', $journal);
echo $journal;
require_once('../inl/connect.php');
$query = "Insert into `journal` (`date`, `time`, `title`, `journal`, `mood`, `who`) Values ('" . $date . "', '" . $time . "', '" . $title . "', '" . $journal . "', '" . $mood . "', '" . $who . "')";
$result = @mysql_query($query) or die('Query failed: ' . mysql_error());
mysql_close();[/PHP]
titaniumdecoy is offline   Reply With Quote
Old Dec 30th, 2005, 9:40 PM   #7
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 172
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
Thank you for so much help, I took the whole
(on the inputted place)
[PHP]$journal = str_replace("\n", '\n', $journal);
$journal = str_replace("\r", '\r', $journal); [/PHP]
with
[PHP]
$journalArray[$i] = ereg_replace("\n", "<br />", $journalArray[$i]);
$journalArray[$i] = ereg_replace("\r", "<br />", $journalArray[$i]);
[/PHP]
On the display page.
PhilBon is offline   Reply With Quote
Old Dec 30th, 2005, 11:16 PM   #8
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 172
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
is there a \t in php/mysql that's a tab?

Last edited by PhilBon; Dec 30th, 2005 at 11:16 PM. Reason: three is spelled wrong, there'
PhilBon is offline   Reply With Quote
Old Dec 31st, 2005, 1:06 AM   #9
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Yes, there is.

[PHP]$journal = str_replace("\t", '\t', $journal);[/PHP]
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC