Jump to content

Problems with mysql_real_escape_string


lynxus

Recommended Posts

Hi Guys,

Im having issues with mysql_real_escape_string

 

For instance, I understand that it only escapes chars, However they just are not entering the DB..

 

For example.

 

$message = "How much is P&P to the UK?";

$message = mysql_real_escape_string($message);

 

 

However when i insert it into a mysql DB, it only inserts "How much is P"

 

Any ideas how i can get this to work.

I want to have these %^&*%$£"!"@ chars in the DB however i want to avoid injections the best way possible.

 

Any help would be grateful.

Thanks

G

Link to comment
https://forums.phpfreaks.com/topic/208763-problems-with-mysql_real_escape_string/
Share on other sites

Heres the entire code:

 

$username=$_SERVER['REMOTE_ADDR']; 
$message = $_GET['message'];
$siteid = $_GET['siteid'];
$owner = $_GET['username'];

// If the user hasnt entered anything just die silently, Stops them from filling screen with empty lines.
if ($message == "") {
die;
}

$error = "0";


$con = mysql_connect("localhost","UNAME","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DB", $con);

$siteid = mysql_real_escape_string($siteid);
$username = mysql_real_escape_string($username);
$owner = mysql_real_escape_string($owner);
$message = mysql_real_escape_string($message);


mysql_query("INSERT INTO data (username, message, owner, siteid, dispname)
VALUES ('$username', '$message', '$owner', '$siteid', '$owner')");


mysql_close($con);

You should use urlencode instead of htmlentities. You'd then use urldecode when grabbing the data from the url.

 

However how are you using a form? If you are you'll be better off setting the forms submit method to post (<form action="" method="post">) rather than get.

Thanks for your help. I never thought about the GET issue.

 

Ive resolved it by doing this in JS ( before sending data )

message = message.replace("&", "%amp");

message = message.replace("+", "%plus");

 

Then in PHP i have:

$message = str_replace("%amp", "&", $message);

$message = str_replace("%plus", "+", $message);

 

Then i escape the string.

 

 

@ Wildteen.

The form is being sent via Ajax. So im using ajax.open(get,url,true);

 

Ive "hacked it" so it works now.

 

I will eventually change the JS / Ajax to use POST

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.