Posting quotation marks from textarea in a form

Discussion in 'PHP' started by domfos, Mar 7, 2007.

  1. #1
    Hi,
    I am fairly new to php and am having a problem with posting a form.

    The form has a text area and the text will sometimes include quotation marks.
    eg: My name is "Earl"

    When I post the form and retrieve the variable using the following code:

    $myvar = $_POST['my_var'];
    Code (markup):
    When I use $myvar, it is equal to:
    My name is \"Earl\"

    What I need to do is get rid of the \ characters, so that I can send the variable to and sql query.

    Can anyone tell me how I would go about doing this?
     
    domfos, Mar 7, 2007 IP
  2. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #2
    papa_face, Mar 7, 2007 IP
  3. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    rays, Mar 8, 2007 IP
  4. php_daemon

    php_daemon Active Member

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #4
    A good idea is to add this snippet to every page, or a page included in every other page:
    
    if (get_magic_quotes_gpc()) {
      foreach ($_GET as $key=>$val){
         $_GET[$key] = stripslashes($val);
      }
      foreach ($_POST as $key=>$val){
         $_POST[$key] = stripslashes($val);
      }
    }
    
    PHP:
    Alternatively you can use a function or method to validate the user input, for example:
    
    function validate($value){
      if (get_magic_quotes_gpc()) {
        return mysql_real_escape_string(stripslashes($value));
      }else{
        return mysql_real_escape_string($value);
      }
    }
    
    PHP:
    The reason behind it is that the magic quotes are depreciated and cannot be trusted. They are turned off in the latest php versions by default and mysql_real_escape_string should be used to validate the data for the sql queries.
     
    php_daemon, Mar 8, 2007 IP
  5. domfos

    domfos Well-Known Member

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    133
    #5
    Thanks for all your help guys. That has sorted my problem now.
    :)
     
    domfos, Mar 8, 2007 IP