mysql_real_escape_string problem

Discussion in 'PHP' started by adsegzy, May 26, 2011.

  1. #1
    Hello Friends,

    am still having problem with mysql_real_escape_string in my forms. whether magic_quotes_gpc is On or Off, i still receive slashes in my entries. someone introduced this code
    mysql_real_escape_string(strip_tags($_POST['first_name']));
    PHP:
    to me but stil end up in the same way. But i came up with the combination of these two

    $name2=stripslashes($_POST[name]);
    $sname=mysql_real_escape_string($name2);
    PHP:
    Pls can i use this or is there a better way of doing it?
     
    adsegzy, May 26, 2011 IP
  2. mlblinco

    mlblinco Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could create a function.

    
    
    <?php
    
    	function protect($value){
    		
    		$value = mysql_real_escape_string($value);
    		$value = strip_tags($value);
    		
    		return $value;
    		
    	}
    	
    	$name = protect($_POST['first_name']);
    
    ?>
    
    
    PHP:
    Then you could just call out the protect function when you are using it to validate a field, its up to you.

    I hope it helps.
     
    mlblinco, May 27, 2011 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    mysql_real_escape/-string will add slashes that are needed for making a string save to be stored in a db field. once it is saved you will no longer see the slashes
     
    stephan2307, May 28, 2011 IP
  4. st8ic

    st8ic Peon

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You have it right, the quick way would be

    mysql_real_escape_string(stripslashes($_POST['name']));
    Code (markup):
    No need for a separate function.
     
    st8ic, May 28, 2011 IP