mysql_real_escape_string change my data

Discussion in 'PHP' started by roice, Oct 31, 2010.

  1. #1
    Hello,
    I'm using ""mysql_real_escape_string" to protect my website, but all the DATA that users sent with the form got change.
    for examle, when they enter "Mike's files" the data in the DB changed to "Mike\'s files"

    How can I avoid that?

    BTW , 'm going to use "htmlspecialchars" too, will the data going to change too/again?

    Thank you in advance
     
    roice, Oct 31, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    That's how mysql_real_escape_string works. It encodes the data before it gets stored in your database to prevent problems. When you use mysql_real_escape_string, you need to use stripslashes() on the data when you retrieve it from your database. Good luck!
     
    rainborick, Oct 31, 2010 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If I'll use "stripslashes" and than I'll print to the screen, there is a chance that harmfull/bad JS will activate...no?
     
    roice, Oct 31, 2010 IP
  4. S1M

    S1M Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    mysql_real_escape_string escapes sql code. If you want to escape javascript, you'll need to do something different. Look up htmlspecialchars() at php.net
     
    S1M, Oct 31, 2010 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm going to use "htmlspecialchars" before printing the data to screen, but how do I fix the text that got change from, example, Mike's file to Mike\'s file ?
     
    roice, Nov 1, 2010 IP
  6. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #6
    You use stripslashes() to change "Mike\'s file" back to "Mike's file". The function mysql_real_escape_string protects your database from malicious sql code, not the user's browser from malacious javascript code.
     
    plog, Nov 1, 2010 IP