昨天,脚本工作得很好,当我填写正确的信息时,我仍然被重定向,但是当我没有时,我得到了以下错误:
注意:未定义变量:第 26 行 C:\xampp\htdocs\webshop\resources\library\login.class.php 中的状态
致命错误:在第 26 行的 C:\xampp\htdocs\webshop\resources\library\login.class.php 中的非对象上调用成员函数 rowCount()
<?php
require_once('../resources/config.php');
class user
{
private $db;
public function __construct()
{
$this->db = new config();
$this->db = $this->db->dbConnect();
}
public function login($name, $pass)
{
if(!empty($name) && !empty($pass))
{
//parameter query to prevent sql injection
$state = $this->db->prepare("select * from users where name=? and pass=?");
$state->bindParam(1, $name);
$state->bindParam(2, $pass);
$state->execute();
}
if($state->rowCount() == 1)
{
header('location: index?page=basket.php');
echo "you have been loged in as <b>$dbuser</b>";
}else{
echo "incorrect user";
}
}
}
?>
用法
<?php
require_once('../resources/library/login.class.php');
if(isset($_POST['submit']))
{
$name = $_POST['user'];
$pass = $_POST['pass'];
$obj_login = new user();
$obj_login->login($name, $pass);
}
?>
<form method="post" action="index.php?page=login.php">
username: <input type="text" name="user"/>
password: <input type="text" name="pass"/>
<input type="submit" name="submit" value="Login"/>
</form>
有人可以指导我吗?