0

我不明白为什么这个方法总是返回 false。我正在检查输入的密码的长度。绝对超过5个字符。输入字段的名称是正确的。但它总是抛出异常throw new Exception("Password must contain at least 6 characters.");

方法:

public function checkPassword(){
        if(strlen($this->post_data['register_password']) > 5){

        }else{
            throw new Exception("Password must contain at least 6 characters.");
        }
    }

调用:

if( isset($_POST['register-submit'])){  
    $error = '';
    $register = new register($_POST, $dbh);
        try {
            if($register->checkUsername()){
                if($register->checkPassword()){

                }
            }
        } catch (Exception $e) {
            $error .= '<span style="color:red;">' . $e->getMessage() . '</span>';
        }
}

编辑:添加了更多的类

public $post_data = array();
private $dbh;
public function __construct($post_data, PDO $dbh){
        $this->error = array();
        $this->post_data = array_map('trim', $post_data);
        $this->dbh = $dbh;

    }
4

2 回答 2

2

您创建类register$_POST然后使用$this->post_datafetch register_password。我怀疑有什么问题$this->post_data,添加var_dump到调试:

public function checkPassword(){
    if(strlen($_POST['register_password']) > 5){
        return true;

    }else{
        // for debug
        // var_dump($this->post_data['register_password']);
        throw new Exception("Password must contain at least 6 characters.");
    }
}

添加var_dump调试__construct

public function __construct($post_data, PDO $dbh){
    $this->error = array();
    $this->post_data = array_map('trim', $post_data);
    var_dump($this->post_data);
    $this->dbh = $dbh;
}
于 2013-09-16T16:32:02.960 回答
2

尝试放在var_dump($_POST)调用代码的顶部。确保您有正确的密码变量名。我注意到您有register-submit提交按钮和register_password密码。您是否将连字符 ( -) 与下划线 ( _) 混合在一起?

于 2013-09-16T16:46:47.930 回答