我是oop的新手,我还没有找到如何在函数Login()中插入$ status的值。我做错了什么???因为我得到这个错误:
警告:缺少User :: Login()的参数3,在第13行的E:\ xampp \ htdocs \ caps \ index.php中调用,并在E:\ xampp \ htdocs \ caps \ class \ user.php中定义第20行
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>";
}
}
}
答案 0 :(得分: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
来使构造函数值成为“默认值”,这样可以在需要时调用函数时覆盖状态值。
答案 1 :(得分:0)
这意味着您调用$ user-&gt; 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);