jeudi 7 janvier 2016

Variables not passing between IF statements PHP

Okay so I'm writing a piece of code which checks a number of conditions which are set in IF statements in order to display a button to signup for a website. However, the variables I am declaring as true/false in my IF statements don't seem to be transferring over to the statement where it determined whether they are all true. Ive looked online and can't seem to find any problem like this anywhere, so i don't know if its me being stupid or there is a genuine problem. Here's my code:

<?php

include_once('db_conx.php');

//CHECKING USERNAME IS NOT TAKEN 

if(!empty($_POST["username"])) {

    $usernameAuth = false; 
    $username = $_POST['username'];

    $sql = "SELECT * FROM users WHERE username='$username'";
    $queryUname = mysqli_query($conn, $sql); 
    $username_check = mysqli_num_rows($queryUname);

    if($username_check>0){
        echo "Username Not Available.";
    } else {
        echo "Username Available.";
        $usernameAuth = true;
    }
}

//CHECKING EMAIL HAS NOT BEEN USED BEFORE

if(!empty($_POST["email"])) {

    $emailAuth = false;
    $email = $_POST['email'];

    $sql = "SELECT * FROM users WHERE email='$email'";
    $queryEmail = mysqli_query($conn, $sql); 
    $email_check = mysqli_num_rows($queryEmail);

    if($email_check>0){
        echo "Email already registered.";
    } else {
        $emailAuth = true;
    }
}

//CHECKING THAT THE PASSWORDS MATCH AND ARE VALID

if(!empty($_POST["password1"]) && !empty($_POST["password2"])) {

    $passAuth = false;
    $password1 = $_POST['password1'];
    $password2 = $_POST['password2'];

    if($password1 != $password2) {
        echo "Your passwords do not match.";
    } elseif(!preg_match("#[0-9]+#",$password1)) {
        echo "Your Password Must Contain At Least 1 Number!";
    } elseif(!preg_match("#[A-Z]+#",$password1)) {
        echo "Your Password Must Contain At Least 1 Capital Letter!";
    } elseif(!preg_match("#[a-z]+#",$password1)) {
        echo "Your Password Must Contain At Least 1 Lowercase Letter!";
    } else {
        $passAuth = true;
    }
}

//CHECKING TO SEE IF ALL THE VALIDATION CONDITIONS ARE MET

if(!empty($_POST["checkinput"])){

    if(($usernameAuth) && ($emailAuth) && ($passAuth)){
        echo '<button id="button" class="btn waves-effect waves-light" type="submit" onclick="signup()" name="action">Register<i class="material-icons right">send</i></button>';
    } else {
        echo 'shite';
    }
}
?>

If you need to know more or want any more pieces of the project, just say and I'm happy to provide them.

Aucun commentaire:

Enregistrer un commentaire