PHP三元返回错误

时间:2017-02-04 12:20:24

标签: php mysqli ternary-operator

好吧所以我有点困惑,这个方法我用来检查某人是否已注册或不工作。

private function isRegistered($username) {
    $sql = $this->connect();

    $sql->real_escape_string($username);

    $res = $sql->query("SELECT username FROM users WHERE username='".$username."'");

    $rows = $res->num_rows;

    $sql->close();

    return ($rows >= 1) ? true : false;
}

对某些人而言,它会返回true,而某些人则会返回false。我的意思是如果他们输入的名称不在数据库中,有时它会返回true eventho它不存在。

我认为这很简单

public function register($username, $pass, $email) {
    if(!$this->isRegistered($username)) {
        $sql = $this->connect();

        $sql->real_escape_string($username);
        $sql->real_escape_string($email);
        $password = hash('sha512', $pass);

        $sql->real_escape_string($password);

        $sql->query("INSERT INTO users (username, password, email) VALUES ('".$username."', '".$password."', '".$email."')");

        $sql->close();

        return true;
    } else {
        return false;
    }
}

执行本身就是这个

$reg = $user->register($_POST['username'],$_POST['password'],$_POST['email']);

if($reg == true) {

    session_start();
    $_SESSION['username'] = $_POST['username'];

    echo "success";
} else {
    echo "acc_exists";
}

0 个答案:

没有答案