Single quotes in user entered fields

Discussion in 'PHP' started by michaelh613, Nov 25, 2007.

  1. #1
    I have a form which asks for information such as name which could have a single quote in it. When I insert it into mySql database the query fails because the field hasn't been escaped. Is there a function or an easy way to check a entry from a form for items like single quotes which cause database entry errors.
     
    michaelh613, Nov 25, 2007 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    you can probably use addslashes or better yet mysql_real_escape_string()
    :D
     
    serialCoder, Nov 25, 2007 IP
  3. Indian_Webmaster

    Indian_Webmaster Banned

    Messages:
    1,289
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can use following functions to get rid of this:
    stripslashes() and addcslashes()

    For Manual, Visit this: http://in.php.net/addslashes

    Hope this helps...
     
    Indian_Webmaster, Nov 25, 2007 IP
  4. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    *louie*, Nov 25, 2007 IP
  5. PowerExtreme

    PowerExtreme Banned

    Messages:
    2,118
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can use following functions stripslashes() and addcslashes()
     
    PowerExtreme, Nov 26, 2007 IP
  6. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Funny, just posted this function in another topic. Cleans arrays and strings. One thing, you need to be connected to MySQL or you'll get some funky errors :)
    
    function clean ($string) {
    	if (is_array($string)) {
    		foreach ($string as $key => $value) {
    			if (function_exists('mysql_real_escape_string')) {
    				$string[$key] = mysql_real_escape_string($value);
    			} else {
    				$string[$key] = addslashes($value);
    			}
    		}
    	} elseif (is_string($string)) {
    		if (function_exists('mysql_real_escape_string')) {
    			$string = mysql_real_escape_string($string);
    		} else {
    			$string = addslashes($string);
    		}
    	}
    	return $string;
    }
    
    PHP:
    To call it, simply do the following:
    
    // as an array
    $array = array('one', 'two', 'three');
    $array = clean($array);
    
    //as a string
    $string = 'hello, world!';
    $string = clean($string);
    
    PHP:
    Have fun.
     
    -NB-, Nov 26, 2007 IP