2 of 3 Error Messages Not Working


I have created a login code which adds the firstname and surname to the database. However when the cells are left blank they do not provide the first 2 error messages below. However the 3rd error messages works. Does anyone have any suggestions why the first two error messages dont work.

    if(isset($_POST['registerSubmit']) && $_POST['registerSubmit'] == 'true'){
    $firstname = mysql_real_escape_string(trim($_POST['firstname']));
    $surname = mysql_real_escape_string(trim($_POST['surname']));
        $registerEmail = trim($_POST['email']);
        $registerPassword = trim($_POST['password']);
        $registerConfirmPassword    = trim($_POST['confirmPassword']);

    if(!isset($firstname) || empty($firstname)) {
        $error = "Please enter your First Name.";
	if(!isset($surname) || empty($surname)) {
        $error = "Please enter your Surname.";
        if(strlen($registerPassword) < 6 || strlen($registerPassword) > 12)
            $errors['registerPassword'] = 'Your password must be between 6-12 characters.';  

Why is that on fail, you populate both either a what seems to be a string variable $error and also an array $errors depending on the failure?

Where do you echo out $error?

Also, this looks very dubious to me:

$_POST['registerSubmit'] == 'true'

When the form is submitted does it really have the string value ‘true’ or are you looking for a boolean true?

When forking your code and things are not going as expected the very first thing you should do is var_dump() the values onto the screen somewhere, or into an error log file – and then carefully inspect the values.

in your case:

// a line of debug
var_dump( $_POST );

Then you should be inclined to use === to make sure those values match your expectations exactly (strict, value and type match), rather than relying on the woolly == (loose, value matches only).

More here: PHP Truth table.