Jump to content

mysql_real_escape_string issue


AdamB

Recommended Posts

Hello,

 

I'm having a problem with mysql_real_escape_string in an application I'm writing. Whenever I escape a string I get an error back because the database cannot then insert it:

 

function add_user($firstname, $lastname, $telephone, $email) {
$query = "INSERT INTO tblusers (firstname, lastname, telephone, email, active) VALUES('$firstname', '$lastname', '$telephone', '$email', '1');";
$query = mysql_real_escape_string($query);
mysql_query ($query) or die ('Could not add caseworker.');
}

 

I know the slashes are being added correctly because I've echo'ed the query post-escaping.

Have I misunderstood the use of the function or is there something else I need to use?

 

Thanks!

 

Adam

Link to comment
https://forums.phpfreaks.com/topic/141224-mysql_real_escape_string-issue/
Share on other sites

You should not escape whole query, you should escape individual variables that go into query instead.

 

function add_user($firstname, $lastname, $telephone, $email) {
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$telephone = mysql_real_escape_string($telephone);
$email = mysql_real_escape_string($email);
$query = "INSERT INTO tblusers (firstname, lastname, telephone, email, active) VALUES('$firstname', '$lastname', '$telephone', '$email', '1');";
mysql_query ($query) or die ('Could not add caseworker.');
}

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.