MandyLB
MandyLB

Reputation: 317

PHP skipping if blocks

When I run this page, everything shows up correctly, but then when I try to test my various error messages, my button keeps redirecting me back to my login page as if everything was inputted correctly. It fails to register the if blocks I've included. Below is the php (the html runs fine, not included).

*Side note, a few lines are commented out because I initially had PDO and am changing them over to mysql, but those shouldn't affect everything else running. I have them commented out too so if things did work, I wasn't adding unnecessary info to my database.

Upvotes: 0

Views: 52

Answers (2)

apokryfos
apokryfos

Reputation: 40663

The problem: Your error may be set, but your INSERT will execute only if $password == $password2 which will be true if they're both empty.

You need to indicate alternative paths by doing else if

<?php
error_reporting (E_ALL);

$error = "";

if (isset($_POST['createAccount'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $password2 = $_POST['password2'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $province = $_POST['province'];
    $postalCode = $_POST['postalCode'];

    if (!$username){
        $error = "<br><div><em>No username entered.</em></div>";
    }
    elseif (!$password || !$password2){
        $error = "<br><div><em>Missing password.</em></div>";
    }
    elseif (!$firstName || !$lastName){
        $error = "<br><div><em>Please enter first and last name.</em></div>";
    }
    elseif (!$address || !$city || !$province || !$postalCode){
        $error = "<br><div><em>Insufficient address provided. Please fill in all fields.</em></div>";
    }
    elseif ($password != $password2){
        $error = "<br><div><em>Passwords do not match.</em></div>";
    }       
    else{

        $conn = mysql_connect(<blocked out for privacy reasons>);
        $db = mysql_select_db("grocery", $conn);

        $account = mysql_query("SELECT * 
                                    FROM accounts 
                                    WHERE username = '$username'",
                                $conn);

        $rowExist = mysql_num_rows($account);

        if ($rowExist == 1){
            $error = "<br><div><em>Username already exists.</em></div>";
        }

        else {

            //$newAccount = ("INSERT INTO accounts (username, password, first_name, last_name, street, city, province, postal_code)
            //                  VALUES ('$username','$password','$firstName','$lastName','$address','$city','$province','$postal_code')");
            //$conn->exec($newAccount);

            header("location: GroceryLogin.php");
        }
        mysql_close($conn);
    }
}   

// I'm guessing here you do an echo $error;

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157870

Of course, PHP is not skipping anything. It is diligently running your conditions, but in your code the only condition that affects the insert is the last one.

To make it work as desired you have to change all your ifs to elseif save for the first one

Upvotes: 4

Related Questions