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 ?
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.
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
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.
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):