vendredi 1 juillet 2016

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.

<?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>";
    }
    if (!$password || !$password2){
        $error = "<br><div><em>Missing password.</em></div>";
    }
    if (!$firstName || !$lastName){
        $error = "<br><div><em>Please enter first and last name.</em></div>";
    }
    if (!$address || !$city || !$province || !$postalCode){
        $error = "<br><div><em>Insufficient address provided. Please fill in all fields.</em></div>";
    }
    if ($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);
    }
}   
?>

Aucun commentaire:

Enregistrer un commentaire