Jump to content

PHP automatically generate a slash when adding record to database


Deepzone

Recommended Posts

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)

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?

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);
}

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.