AdamB Posted January 17, 2009 Share Posted January 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141224-mysql_real_escape_string-issue/ Share on other sites More sharing options...
Mchl Posted January 17, 2009 Share Posted January 17, 2009 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.'); } Quote Link to comment https://forums.phpfreaks.com/topic/141224-mysql_real_escape_string-issue/#findComment-739165 Share on other sites More sharing options...
DarkWater Posted January 17, 2009 Share Posted January 17, 2009 You completely misunderstood how to use the function. You don't escape the whole query string. You need to escape all of the individual variables you're using in the query. Quote Link to comment https://forums.phpfreaks.com/topic/141224-mysql_real_escape_string-issue/#findComment-739167 Share on other sites More sharing options...
PFMaBiSmAd Posted January 17, 2009 Share Posted January 17, 2009 Individual pieces of data are escaped, not the whole query as that would break the quotes that are part of the query syntax. Quote Link to comment https://forums.phpfreaks.com/topic/141224-mysql_real_escape_string-issue/#findComment-739169 Share on other sites More sharing options...
.josh Posted January 17, 2009 Share Posted January 17, 2009 mysql_real_escape_string sucks. Be a man. Use regex. It'll put hair on your chest. Quote Link to comment https://forums.phpfreaks.com/topic/141224-mysql_real_escape_string-issue/#findComment-739178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.