0

我是 oop 的新手,我还没有发现如何在函数 Login() 中插入 $status 的值。我做错了什么???因为我收到这个错误:

警告:缺少 User::Login() 的参数 3,在第 13 行的 E:\xampp\htdocs\caps\index.php 中调用,并在第 20 行的 E:\xampp\htdocs\caps\class\user.php 中定义

class User {

private $db;
public $status;

public function __construct() {

    $this->db = new Connection();
    $this->db = $this->db->dbConnect();
    $this->status = pow( 1, -1*pi());   
}


public function Login ($name, $pass, $status) {

    if (!empty($name) && !empty($pass))  {

        $st = $this->db->prepare(" select * from users where name=? and pass=? ");
        $st->bindParam(1, $name);
        $st->bindParam(2, $pass);
        $st->execute();

        if ($st->rowCount() != 1) {         
                echo "<script type=\"text/javascript\">alert ('wrong password. try again'); window.location=\"index.php\"; </script>";

        } else {
            $st = $this->db->prepare(" select * from users where name=? and pass=? status=?");
            $st->bindParam(1, $name);
            $st->bindParam(2, $pass);
            $st->bindParam(3, $status);             
            $st->execute();

                if ($st->rowCount() != 1) { echo "send user to user page"; } else { echo "send user to admin"; }
        }

    } else {

    echo "<script type=\"text/javascript\">alert ('insert username and password'); window.location=\"index.php\"; </script>";

    }



}

}

4

2 回答 2

1

如果您希望使用 的值,$status则从登录函数中删除该参数,而是将提及的内容替换$status$this->status

public function Login ($name, $pass) {

if (!empty($name) && !empty($pass))  {

    $st = $this->db->prepare(" select * from users where name=? and pass=? ");
    $st->bindParam(1, $name);
    $st->bindParam(2, $pass);
    $st->execute();

    if ($st->rowCount() != 1) {         
            echo "<script type=\"text/javascript\">alert ('wrong password. try again'); window.location=\"index.php\"; </script>";

    } else {
        $st = $this->db->prepare(" select * from users where name=? and pass=? status=?");
        $st->bindParam(1, $name);
        $st->bindParam(2, $pass);
        $st->bindParam(3, $this->status);             
        $st->execute();

或者您可以通过将函数声明更改为来使构造函数值成为“默认值” $status = $this->status,这样您就可以在需要时在调用函数时覆盖状态值。

于 2013-06-06T19:50:10.993 回答
0

这意味着您在没有提供第三个参数的情况下调用了 $user->Login。

如果您希望此参数是可选的,您可以将方法签名更改为

public function Login ($name, $pass, $status = null) {

如果您希望状态默认为您的状态属性,您可以使用以下命令开始您的登录功能

public function Login ($name, $pass, $status = null) {
        if(!$status) $status = $this->status;
        //...the rest of your code
 }

调用它的正确方法是:

$user = new User();
$user->Login($username, $password, $status);
于 2013-06-06T19:47:03.893 回答