如果语句没有填充错误数组php

时间:2017-09-25 05:00:44

标签: php mysql

好吧我想要完成的是让php一次吐出多个错误,这样对用户来说并不烦人。

示例:检查一个检查用户名是否存在,检查两个检查密码是否匹配,检查三个检查密码是否为8个字符或更多。注册表中的用户输入形成一个不存在的用户名,一个不匹配的密码和一个只有5个字符长的密码。这将是三个不同的错误。我试图一次输出所有三个,所以用户立即看到错误。而不是输出每个人和用户必须返回更改,然后另一个返回更改,然后另一个返回更改,在这种情况下。

我觉得我的if语句设置错误。我知道这是一个用户错误,但我不知道它是什么。发生的事情是,当我触发一个错误时,它工作正常,但如果我触发多个错误数组是空的。

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)

这不是我整合问题区域的所有代码。代码的其余部分只是一个foreach循环迭代错误,如果它是空的,如果它是插入用户到db

3 个答案:

答案 0 :(得分:1)

我不确定为什么在fetch查询期间使用while循环。如果您一次显示所有错误,那么您可以使用以下代码:(虽然未经过测试)让我知道它的工作与否。

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

答案 1 :(得分:0)

更新了解决原始问题的代码:

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

    }

答案 2 :(得分:0)

获取输出数组中所有值的解决方案:

    if(!empty($errors)){

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

    }