PHP- Problem with a string

Discussion in 'PHP' started by ItamarP, Nov 20, 2010.

  1. #1
    hey there
    
    i wrote 2 validation functions for a registration form i've made
    
    
    function check_user($user,$error)
    { 
        //check if user name already exist
        db_login();
        $row=mysql_query("SELECT * FROM users") or die(mysql_error());
        while(mysql_fetch_array($row))
            {
                if($row['username']==$user)
                    $error.=" username already exist ";
            }
            
            return $error ; 
            
        
        //check if user field is filled with spaces
        $user=trim($user);
        if(empty($user))
        $error.="username must be alphabetic/numeric/combination of both" ;
    }
    
    function check_pass($pass,$pass2,$error)
    { //check- is $pass==$pass2 ?
        if($pass!=$pass2)
            $error.=" Passwords are not the same ";
            
            return $error;
    }
    
    
    $error=""; //error list 
    $error.=check_pass($pass,$pass2,$error);
    $error.=check_user($user,$error);
    
    
    Code (markup):


    if($error="")
    echo "none";
    else
    echo $error."please fix the problems mentioned above";


    the output is "please fix the problems mentioned above" - but i don't see any of the errors.....

    what's the solution ?
     
    ItamarP, Nov 20, 2010 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    download php login v2.3 script from:

    http://php-login-script.com/

    take a look at the files dbc.php and checkuser.php. Depending on your circum stances you may not have to reinvent the wheel. These scripts have some good functions for validating user input. You may be able to 'modify' some of them to meet your needs. Or at least get some inspiration. it has functions for email validation url, validation, and user name validation:) free for personal and commercial. not to be sold.
     
    shofstetter, Nov 20, 2010 IP
  3. namduong8889

    namduong8889 Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hello there... you made a mistake,see the red line, it should be if ($error == '')


    by the way i have some suggestions with your code:
    - function check_user() is missing return
    - to check if an username exists... try this better:
    
    // check username exists?
    $test = mysql_query("select * from `users` where `username` = '$user'"); 
    if (mysql_num_rows($test)) return false; else return true;
    
    Code (markup):
    - the $error variable which holds error information should be an array, rather than string, e.g.:

    
    $error[] = "username must be alphabetic/numeric/combination of both";
    
    Code (markup):
    so you can fetch the error information easier
     
    Last edited: Nov 20, 2010
    namduong8889, Nov 20, 2010 IP
  4. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #4
    I think you want this portion of the code
    
        return $error ; 
            
        
        //check if user field is filled with spaces
        $user=trim($user);
        if(empty($user))
        $error.="username must be alphabetic/numeric/combination of both" ;
    
    PHP:
    to be like this
    
        //check if user field is filled with spaces
        $user=trim($user);
        if(empty($user))
        $error.="username must be alphabetic/numeric/combination of both" ;
    
        return $error ; 
    
    PHP:
    Edit: also what @namduong8889 said.
     
    Gray Fox, Nov 20, 2010 IP
  5. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #5
    You could also give this a try:

    
    function validate($user,$pass,$pass2){
    $error= array(); //error list 
        //check if user name already exist
        db_login();
        $row=mysql_query("SELECT * FROM users") or die(mysql_error());
        while(mysql_fetch_array($row))
            {
                if($row['username']==$user)
                    $error[] = " username already exist ";
            }
              
         //check if user field is filled with spaces
        $user=trim($user);
        if(empty($user))
        $error[] = "username must be alphabetic/numeric/combination of both" ;
    
     //check- is $pass==$pass2 ?
        if($pass!=$pass2)
            $error[] = " Passwords are not the same ";
            
            return $error;
    }
    
    
    $errors = validate($user,$pass,$pass2);
    
    if(empty($errors)){
    	echo "none";
    }else{
    	foreach ($errors as $err) {
    	    echo $err."<br />\n";
    	}
    }
    
    Code (markup):
     
    shofstetter, Nov 20, 2010 IP