![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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> |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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] |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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.
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#6 |
|
Expert Programmer
|
[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] |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
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' |
|
|
|
|
|
#9 |
|
Expert Programmer
|
Yes, there is.
[PHP]$journal = str_replace("\t", '\t', $journal);[/PHP] |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|