Deepzone Posted March 23, 2013 Share Posted March 23, 2013 Can anyone tell me how to prevent a slash from auto-generated when inserting a string that has a apostophe. Example can't become can\'t. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/276056-php-automatically-generate-a-slash-when-adding-record-to-database/ Share on other sites More sharing options...
PaulRyan Posted March 23, 2013 Share Posted March 23, 2013 (edited) That what escaping data does, you have to remove it via stripslashes when outputting to the end user. Edited March 23, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/276056-php-automatically-generate-a-slash-when-adding-record-to-database/#findComment-1420540 Share on other sites More sharing options...
Barand Posted March 23, 2013 Share Posted March 23, 2013 No, Paul, you shouldn't be writing it to the database in the first place. If you have magic_quotes ON then a slash is added for you automatically, so it is received in the POST as can\'t. If you then escape it with real_escape_string it becomes can\\\'t, which then gets written to the database as can\'t. To avoid it, use stripslashes if magic quotes is ON before escaping. This way the escaped value is can\'t which is written to the db as can't (correctly) Quote Link to comment https://forums.phpfreaks.com/topic/276056-php-automatically-generate-a-slash-when-adding-record-to-database/#findComment-1420541 Share on other sites More sharing options...
Deepzone Posted March 23, 2013 Author Share Posted March 23, 2013 I debugged it and found the slash was generated by real_escape_string function. I use this function to sanitize the capture to prevent sql injection. So shall I use the stripslashes after this function? But then if user does need to input a slash, what do I do? Quote Link to comment https://forums.phpfreaks.com/topic/276056-php-automatically-generate-a-slash-when-adding-record-to-database/#findComment-1420543 Share on other sites More sharing options...
PaulRyan Posted March 23, 2013 Share Posted March 23, 2013 Barand, thanks for pointing that out. I just assumed that is what happened, I've had magic_quotes on all along it seems, must have forgotten to turn them off with my new install on my computer. Quote Link to comment https://forums.phpfreaks.com/topic/276056-php-automatically-generate-a-slash-when-adding-record-to-database/#findComment-1420546 Share on other sites More sharing options...
Jessica Posted March 23, 2013 Share Posted March 23, 2013 Strip slashes only removes the escape slashes like \' Quote Link to comment https://forums.phpfreaks.com/topic/276056-php-automatically-generate-a-slash-when-adding-record-to-database/#findComment-1420553 Share on other sites More sharing options...
Barand Posted March 23, 2013 Share Posted March 23, 2013 I debugged it and found the slash was generated by real_escape_string function. I use this function to sanitize the capture to prevent sql injection. So shall I use the stripslashes after this function? But then if user does need to input a slash, what do I do? use something like this function sanitize($data) { if (get_magic_quotes_gpc()) { $data = stripslashes($data); } return mysqli_real_escape_string($data); } Quote Link to comment https://forums.phpfreaks.com/topic/276056-php-automatically-generate-a-slash-when-adding-record-to-database/#findComment-1420557 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.