user1886385
user1886385

Reputation: 37

basic php else if not working

I have some php validation for a user signup form. It's validating all the input then if all is correct the else at the end, checks to see if that username is in use and if not creates that record in the database. For some reason the last else doesn't get activated and it just refreshes with all the data still in the input boxes. I can't find the problem anywhere!!

if(isset($_POST['user']))
{
    $firstname = sanitiseString($_POST['firstname']); 
    $surname = sanitiseString($_POST['surname']);
    $user = sanitiseString($_POST['user']);
    $pass = sanitiseString($_POST['pass']);
    $email = sanitiseString($_POST['email']);
    $dateOfBirth = sanitiseString($_POST['dateOfBirth']);
    $gender = sanitiseString($_POST['gender']);

    $test_arr  = explode('-',$dateOfBirth);

    if($firstname == "" || $surname =="" || $user == "" || $pass == "" || $email == "" || $dateOfBirth == "" || $gender == "")
        {$error = "Not all fields were entered</br></br>";}
        else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            {$error = "Email format invalid</br></br>";}
        else if(count($test_arr) == 3) 
            {
            if (!checkdate($test_arr[0], $test_arr[1], $test_arr[2]))
                {$error = "Enter a date in the format: MM-DD-YYYY</br></br>";}
            }
        else if(count($test_arr) <> 3) 
            {$error = "Enter a date in the format: MM-DD-YYYY</br></br>";}
        else
            {
            $result = querySQL("SELECT * FROM members WHERE user='$user'");
            if($result->num_rows)
                {$error = "That Username already exists</br></br>";}
            else
                {
                querySQL("INSERT INTO members VALUES('','$firstname','$surname','$user','$pass','$email','$dateOfBirth','$gender')");
                die("<h4>Account Created</h4>Please Log In.</br></br>");
                }
            }
}

Upvotes: 1

Views: 98

Answers (1)

jhansen
jhansen

Reputation: 284

First thing to comment on is the incredible amount of nested logic this script has; it's not uncommon to lose control of the flow when you're if / else branching gets out of control.

Example Restructure

if (isset($_POST['user']))
{
    // Prep
    $error = '';

    // Sanitize
    foreach( $_POST as $varName => $value )
    {
        // Doing this for minification on Stackoverflow
        $$varName = sanitiseString($_POST[$varName]);

        // Validate
        if ( empty($$varname) )
            $error .= "Not all fields were entered<br /><br />";
    }

    // Valid Email?
    if ( !filter_var($email, FILTER_VALIDATE_EMAIL) )
        $error .= "Email format invalid<br /><br />";

    // Validate date
    $dateArray = explode('-', $dateOfBirth);
    if (!checkdate($dateArray[0], $dateArray[1], $dateArray[2]))
    {
        $error .= "Enter a date in the format: MM-DD-YYYY</br></br>";
    }

    $result = querySQL("SELECT * FROM members WHERE user='$user'");
    if ($result->num_rows)
    {
        $error .= "That Username already exists</br></br>";
    }

    if ( !empty($error) )
        die($error);

    querySQL("INSERT INTO members VALUES('','$firstname','$surname','$user','$pass','$email','$dateOfBirth','$gender')");
    die("<h4>Account Created</h4>Please Log In.</br></br>");
}

Some other things to note are conflicting logic with your count($test_arr) == 3 and count($test_arr) <> 3. And the value of $result->num_rows may not be 0, as your expecting.

Upvotes: 2

Related Questions