Jump to content

PHP automatically generate a slash when adding record to database


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.