Daniel Johanson Daniel Johanson - 1 month ago 10
MySQL Question

If statement not populating errors array php

Okay what I am trying to accomplish is have php spit out more than one error at a time so it isn't annoying to the user.

Example: check one checks username exists, check two checks if password match, check three checks if password is 8 characters or more. User inputs in register form a username that doesn't exist, a password that doesn't match and a password that is only 5 characters long. That would be three different errors. I am trying to output all three at once so user sees whats wrong at once. Instead of outputting each individual and the user having to go back change, then another go back change, and then another go back change, in this scenario that is.

I have a feeling that my if statements are setup wrong. I know this is a user error, I don't know what it is though. What is happening is when I trigger just one error it works just fine, but if I trigger more than one the errors array is empty.

if(isset($_POST['rsn']) && isset($_POST['pw']) && isset($_POST['con_pw'])){

$rsn = preg_replace('/\s+/u', '', $_POST['rsn']);

$pw = $_POST['pw'];

$hashed_pw = password_hash($pw, PASSWORD_BCRYPT);

$con_pw = $_POST['con_pw'];

$query = $conn->prepare("SELECT rsn, rank_num, active FROM users WHERE rsn = :rsn");

$query->execute(array(":rsn" => $rsn));

$error = array();

while($row = $query->fetch(PDO::FETCH_ASSOC)){

$db_rsn = $row['rsn'];

$rank_num = $row['rank_num'];

$active = $row['active'];

if($rsn != $db_rsn){

$error[] = "Sorry, but the rsn: $rsn doesn\'t exist in or database";

if($pw != $con_pw){

$error[] = "Sorry, but your passwords dont match";

if($pw < 8){

$error[] = "Sorry, but your password must be 8 or more characters";

if($rank_num != "7" || $rank_num != "8" || $rank_num != "9" || $rank_num != "10" || $rank_num != "11" || $rank_num != "12"){

$error[] = "Sorry, but you dont have the permissions to register";

}

}

}

}

}

print_r($error);//output = array()

var_dump($error);//output = array(0)


This isn't all the code as I consolidated the problem area. The rest of the code is just a foreach loop iterating over error if it is not empty if it is insert user into db

Answer Source

I am not sure why you are using while loop during fetch query . If you show all errors at once , then you may use below code : (not tested though) Let me know its working or not .

if(isset($_POST['rsn']) && isset($_POST['pw']) && isset($_POST['con_pw'])){

$rsn =  preg_replace('/\s+/u', '', $_POST['rsn']);

$pw = $_POST['pw'];

$hashed_pw = password_hash($pw, PASSWORD_BCRYPT);

$con_pw = $_POST['con_pw'];

$query = $conn->prepare("SELECT rsn, rank_num, active FROM users WHERE rsn = :rsn");

$query->execute(array(":rsn" => $rsn));

$error = array();

while($row = $query->fetch(PDO::FETCH_ASSOC)){

    $db_rsn = $row['rsn'];

    $rank_num = $row['rank_num'];

    $active = $row['active'];
    }
    if($rsn != $db_rsn){

        $error[] = "Sorry, but the rsn: $rsn doesn\'t exist in or database";
   }

   if($pw != $con_pw){

            $error[] = "Sorry, but your passwords dont match";
   }

   if($pw < 8){

                $error[] = "Sorry, but your password must be 8 or more characters";
   }

   if($rank_num != "7" || $rank_num != "8" || $rank_num != "9" || $rank_num != "10" || $rank_num != "11" || $rank_num != "12"){

                    $error[] = "Sorry, but you dont have the permissions to register";

   }

}


print_r($error);//output

var_dump($error);//output