giannis Posted August 29, 2010 Share Posted August 29, 2010 Hello Guys, great forum, first time posting here :-) I'm creating a php registration form, and I'm doing some validations. The problem on my code is the following: When the password is mimatched with the password_again, the following if is executed (referring the email validation). But when the password is matched with the password_again, the email validation isn't running. Why php is not executing the if when the passwords are OK? <?php if(!empty($_POST['username']) && !empty($_POST['password'])) { $username = mysql_real_escape_string($_POST['username']); $password = md5(mysql_real_escape_string($_POST['password'])); $email = mysql_real_escape_string($_POST['email']); $check_username = mysql_query("SELECT * FROM users WHERE username = '".$username."'"); if(mysql_num_rows($check_username) == 1) { echo "<h1>Error</h1>"; echo "<p> Username is already in use, click <a href=\"register.php> here</a> to try again!</p>"; } if($_POST['password'] === $_POST['password_again']) { return true; } else { echo "Passwords do not match. Please click <a href=\"register.php\">here</a> to try again<br/>"; } if (isset($_POST['email'])) { if (isValidEmail($_POST['email'])) { return true; } else { echo "<p><tr><td>The email: ".$_POST['email']." is invalid!</td></tr> Please click <a href=\"register.php\">here</a> to try again</p>"; } } else { $write = mysql_query("INSERT INTO users (username, password, email) VALUES('".$username."', '".$password."', '".$email."')"); if($write) { echo "<p> Account created! Click <a href=\"index.php\">here</a> to login.</p>"; echo "<meta http-equiv='refresh' content='=4;index.php' />"; } else { echo "<p> There was an error. Please click <a href=\"register.php\">here</a> to try again.</p>"; } } } else { ?> Link to comment https://forums.phpfreaks.com/topic/212020-problem-on-validation-with-php/ Share on other sites More sharing options...
DavidAM Posted August 29, 2010 Share Posted August 29, 2010 return true; When the passwords match, you are immediately exiting the function, so none of the following statements will be executed. Link to comment https://forums.phpfreaks.com/topic/212020-problem-on-validation-with-php/#findComment-1104957 Share on other sites More sharing options...
giannis Posted August 29, 2010 Author Share Posted August 29, 2010 I think that you missunderstood. The problem is that when the script checks the passwords and they are matching, the checking doesn't continue to the email Link to comment https://forums.phpfreaks.com/topic/212020-problem-on-validation-with-php/#findComment-1104964 Share on other sites More sharing options...
DavidAM Posted August 29, 2010 Share Posted August 29, 2010 if($_POST['password'] === $_POST['password_again']) { return true; } That code - which I copied from your original post - says: "If the passwords match, RETURN - leave - quit - do NOT run the email check which is the next statement" Link to comment https://forums.phpfreaks.com/topic/212020-problem-on-validation-with-php/#findComment-1104981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.