2

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

4
  • dont nest the if's have each one separate if(){} if(){} if(){} Commented Sep 25, 2017 at 5:07
  • why you have while loop ? shouldn't be single result ? Also no need to use nested if and password character should be first condition . Check query has value or not then assign & compare Commented Sep 25, 2017 at 5:11
  • Okay that fixed the error array not populating, but it isn't outputing all the errors. foreach($error as $errors) {echo $errors . "<br />}; that is the foreach loop that is outputting errors Commented Sep 25, 2017 at 5:17
  • The while loop is also providing the variable $rank_num and checking if user is already registered by the provided value of active Commented Sep 25, 2017 at 5:33

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

The while loop is to test the user provided rsn against database rsn. It is to restrict users that are not apart of group from registering
see @Jigar Shah comment.
I took everything out of the while loop and everything works right except the foreach loop isn't outputting all the errors.
0

Updated code which solves the original question:

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();

$db_rsn = "";

$rank_num = "";

$active = "";

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";

    }

Comments

0

Solution to getting all values in an array outputted:

    if(!empty($errors)){

      echo implode("<br />", $errors);//<br /> is for a new line break in between each value of array

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.