tags:

views:

40

answers:

2

I'm using mysql_real_escape_string to cleanse data that's used in insert and update queries (postal addresses). But thing is, when I have something like this "25\5" the output ends up as 25. Mysql removes whats after the 25. I know it can be entered as 25/5, but some users may deliberately enter it as 25\5. How can I keep and retrieve it as 25\5 itself. Thanks.

Edit:

$address = "23\5, Elm Street";
$clean = mysql_real_escape_string($address);
$data = mysql_query("insert into students (address) values ('$clean') ");

if (!$data) {
    echo "Not ok" .mysql_error();
}else {
    echo "Ok";
} $data = mysql_query("select address from students");

while ($info=mysql_fetch_assoc($data)){
    print $info["address"];
    print '<br>';
}
A: 

Before you escape it, you can either replace the backslash with an escaped backslash or with a forward slash.

$cleared = str_replace("\\", "\\\\", $input);
// or
$cleared = str_replace("\\", "/", $input);
Bobby
I did that. But 23/5 still shows up as 23.
Norman
A: 

The problem was here: "25\5, Elm Street"

I changed it to '25\5, Elm Street' ->Note the single quotes.

It also works ok when used with a form.

Works ok now with only the mysql_real_escape_string function.

Thanks to all who posted :)

Norman