1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to strip quotes

Discussion in 'PHP' started by bobby9101, Nov 28, 2006.

  1. #1
    I want to escape ' and ". I can't figure out how to do this.
    I read up on PHP.net and couldn't figure it out.
    I guess the easiest way is to just add a \ to them, but how to do that?
    BTW this is to stop some innocent SQL injection on my site.
     
    bobby9101, Nov 28, 2006 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can use the function addslashes() to escape all of the " and ' characters. You will then need to use stripslashes if you want to remove them later.
     
    jestep, Nov 28, 2006 IP
  3. wmtips

    wmtips Well-Known Member

    Messages:
    598
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #3
    wmtips, Nov 28, 2006 IP
  4. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The proper way to prevent SQL injection is to use parameterised queries, supported by libraries such as PDO and PEAR::MDB2. That way you don't have to muck around escaping anything.
     
    penagate, Nov 28, 2006 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Use this:
    <?php
    $value = stripslashes($value);
    // to strip the slash.
    $value = mysql_real_escape_string($value);
    // to add slash.
    ?>

    You might also want to check your "magic" settings. Using this way of adding slash will "double" slash if "magic" is "ON".

    Bye :)
     
    JEET, Nov 28, 2006 IP
  6. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sorry, how do I use: $value = mysql_real_escape_string
    so I need to change $variable?

    I have $_POST['desc']
    that needs to be stripped
     
    bobby9101, Nov 29, 2006 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    You could use:

    
    $value = mysql_real_escape_string($_POST['desc']);
    
    //or 
    
    $_POST['desc'] = mysql_real_escape_string($_POST['desc']);
    
    PHP:
     
    jestep, Nov 29, 2006 IP
  8. wmburg

    wmburg Active Member

    Messages:
    300
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #8
    I use and recommend this function:

    
    
      function sql_quote($value)
      {
        if(get_magic_quotes_gpc())
        {
          $value = stripslashes($value);
        }
        // check if this function exists
        if(function_exists("mysql_real_escape_string"))
        {
          $value = mysql_real_escape_string($value);
        }
        // for PHP < 4.3.0 use addslashes
        else
        {
          $value = addslashes($value);
        }
        return $value;
      }
    
    
    PHP:
    So your code would be:

    
    
      $desc = sql_quote($_POST['desc']);
    
    
    PHP:
    Hope this helps.
     
    wmburg, Nov 29, 2006 IP