fullyloaded Posted August 15, 2012 Share Posted August 15, 2012 Hi, I've been working on my sign up form and am having a few problems getting it to work. First problem i'm having is this error: Fatal error: Call to undefined function validadres() in on line 22 i think it has to do with the function being in the wrong place but could be wrong. My second problem is the errors array, if i leave any of the text box fields empty or if the passwords don't match it will still add the user to the database and not show the error messages. Thanks. require("db.php"); if(!empty($_POST)) { $errors = array(); if ($user == "") { $errors[] = "Please Enter A Username."; } if ($pass == "") { $errors[] = "Please Enter A Password."; } if ($confirmpass == "") { $errors[] = "Please Enter A confirmation password."; } if ($email == "") { $errors[] = "Please Enter Your E-mail Address."; } if ($comfirmemail == "") { $errors[] = "Please Enter Your Confirmation E-mail Address."; } if ($gender == "") { $errors[] = "Please Select A Gender."; } if ($month == "") { $errors[] = "Please Select A Month."; } if ($day == "") { $errors[] = "Please Select A Day."; } if ($year == "") { $errors[] = "Please Select A Year."; } if ($country == "") { $errors[] = "Please Select A Country."; } if ($firstname == "") { $errors[] = "Please Enter Your First Name."; } if ($lastname == "") { $errors[] = "Please Enter Your Last Name."; } if (strlen($pass) < $passLengthMIN ) { $errors[] = "The password contains to little chars."; } if (strlen($pass) > $passLengthMAX ) { $errors[] = "The password contains to much chars."; } if (strlen($user) < $userLengthMIN ) { $errors[] = "The username contains to little chars."; } if (strlen($user) > $userLengthMAX ) { $errors[] = "The username contains to much chars."; } if (validadres($email) == false ) { $errors[] = "The given e-mail address is not valid."; } if ($pass <> $confirmpass) { $errors[] = "Passwords do not match."; } if ($email <> $comfirmemail) { $errors[] = "Email Address do not match."; } $query = "SELECT id FROM users WHERE user = :user Or email = :email"; $query_params = array( ':user' => $_POST['user'], ':email' => $email ); try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } $row = $stmt->fetch(); if ($row = $stmt->fetch($result)){ if ($row["user"] == $user) { $errors[] = "Your username is already used by another member."; } if ($row["email"] == $email) { $errors[] = "Your e-mail address is already registrated in our database."; } } if ($errors) { $errorstr = "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>"; } else { $errorstr = ""; } $query = "INSERT INTO users (user,pass,salt,email,month,day,year,firstname,lastname,gender,country ) VALUES (:user,:pass,:salt,:email,:month,:day,:year,:firstname,:lastname,:gender,:country)"; $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); $pass = hash('sha256', $_POST['pass'] . $salt); $query_params = array( ':user' => $_POST['user'], ':pass' => $pass, ':salt' => $salt, ':email' => $_POST['email'], ':month' => $_POST['month'], ':day' => $_POST['day'], ':year' => $_POST['year'], ':firstname' => $_POST['firstname'], ':lastname' => $_POST['lastname'], ':gender' => $_POST['gender'], ':country' => $_POST['country'] ); try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } /////////EMAIL VALID CODE//////////////////////// function validadres($email){ $prereturn = true; if (strlen($email) < 5){$prereturn = false;} $partsNumber = split("@",$email); if (count($partsNumber) <> 2) {$prereturn = false;} else{ list($user,$domain) = split("@",$email); if (strlen($user) < 1) {$prereturn = false;} } return $prereturn; } //////END////////////////////////////////////////// header("Location: login"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/267133-fatal-error-and-error-array-problem/ Share on other sites More sharing options...
jazzman1 Posted August 15, 2012 Share Posted August 15, 2012 Start from here: if (validadres($email) == false ) { $errors[] = "The given e-mail address is not valid."; } You can not assign boolean or some values like this to the function. This is wrong! Start to re-design the script, I found deprecated php functions too. Quote Link to comment https://forums.phpfreaks.com/topic/267133-fatal-error-and-error-array-problem/#findComment-1369670 Share on other sites More sharing options...
xyph Posted August 15, 2012 Share Posted August 15, 2012 Start from here: if (validadres($email) == false ) { $errors[] = "The given e-mail address is not valid."; } You can not assign boolean or some values like this to the function. This is wrong! Start to re-design the script, I found deprecated php functions too. This isn't an assignment, it's a comparison. The issue is the function being declared within the if/else blocks. If you want it pre-parsed before the rest of the code, it must be outside any conditionals. If you want it conditionally declared, you're going to have to declare it BEFORE using it. Quote Link to comment https://forums.phpfreaks.com/topic/267133-fatal-error-and-error-array-problem/#findComment-1369672 Share on other sites More sharing options...
jazzman1 Posted August 15, 2012 Share Posted August 15, 2012 This isn't an assignment, it's a comparison. Sorry I skipped the first one Quote Link to comment https://forums.phpfreaks.com/topic/267133-fatal-error-and-error-array-problem/#findComment-1369678 Share on other sites More sharing options...
ManiacDan Posted August 15, 2012 Share Posted August 15, 2012 Fatal error: Call to undefined function validadres() in on line 22 "Address" is spelled wrong, that just bothers me because you'll never know the names of your functions if they're all spelled wrong. Define all your functions in the global scope at the top of the file (or in a separate include file) so you know they're always accessible. Quote Link to comment https://forums.phpfreaks.com/topic/267133-fatal-error-and-error-array-problem/#findComment-1369680 Share on other sites More sharing options...
DavidAM Posted August 15, 2012 Share Posted August 15, 2012 The reason the user gets inserted is because that's what you told it to do. The INSERT statements are outside of the if test. So, the INSERT will always be executed. if ($errors) { $errorstr = "<ul><li>" . implode("</li><li>", $errors) . "</li></ul>"; } else { $errorstr = ""; } $query = "INSERT INTO users (user,pass,salt,email,month,day,year,firstname,lastname,gender,country ) VALUES (:user,:pass,:salt,:email,:month,:day,:year,:firstname,:lastname,:gender,:country)"; Also: Where are you getting $user, et. al. from? if(!empty($_POST)) { $errors = array(); if ($user == "") { $errors[] = "Please Enter A Username."; } if ($pass == "") { $errors[] = "Please Enter A Password."; } That looks suspiciously like register_globals is on. THIS IS A BAD THING. Even with register_globals OFF, you need to trim those fields. Otherwise, a user can just enter spaces. So, it should be: if (trim($_POST['user']) == '') { #error condition Also: You are SELECTing the id from the users table when checking for an existing username or email address. But you are then checking the user column and the email column, neither of which is there. You are also double-fetching the results which will put you past the end of the result set if there is only one result, and you are not checking all of the results if there are more than 1 (uhh, I mean 2). $query = "SELECT id FROM users WHERE user = :user Or email = :email"; # # ... snip ... # $row = $stmt->fetch(); if ($row = $stmt->fetch($result)){ if ($row["user"] == $user) { $errors[] = "Your username is already used by another member."; } if ($row["email"] == $email) { $errors[] = "Your e-mail address is already registrated in our database."; } } Work on your indentation. It will make the code much easier to read. I completely missed the fact that validadres is inside of the if block. In fact, you may not have meant to put it there in the first place. You are asking the user for a lot of information. And then you die if any of it is bad. This means they have to find their way back to the registration page, and re-type everything. Since they have no control over what user names are already in use, this will get frustrating and cause people to give up trying to register, loosing you potential users. Learn about "sticky fields". Basically, you want the form and processing in the same script, so you can reshow the form along with any error messages and all of the data that the user already entered. I'm not hating on your code. I have made every one of these errors at one time or another. Actually, I'm kind of glad my first development server crashed and lost the hard drive. I'll never again have to look at the garbage I wrote when I was learning PHP. Quote Link to comment https://forums.phpfreaks.com/topic/267133-fatal-error-and-error-array-problem/#findComment-1369737 Share on other sites More sharing options...
fullyloaded Posted August 18, 2012 Author Share Posted August 18, 2012 Hi, thanks everyone for all the help, it helped a lot. Quote Link to comment https://forums.phpfreaks.com/topic/267133-fatal-error-and-error-array-problem/#findComment-1370415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.