所以就像在标题中提到的那样,它给了我错误:'致命错误:调用布尔值的成员函数count()第20行'
我尝试做的是当用户按下注册按钮时,它会引用此文件进行一些验证,以查看它是否存在,是否有效等。
这是故障文件的源代码。
user.php的
<?php
class User {
private $_db, $_data;
public function __construct($user = null) {
$this->_db = DB::getInstance();
}
public function create($fields = array()){
if(!$this->_db->insert('users', $fields)) {
throw new Exception('There was a problem creating user');
}
}
public function find($user = null) {
if($user) {
$fields = (is_numeric($user)) ? 'id' : 'username';
$data = $this->_db->get('users', array($fields, '=', $user));
if($data->count()) {
$this->_data = $data->first();
return true;
}
}
return false;
}
public function login($username = null, $password = null) {
$user = $this->find($username);
print_r($_data);
return false;
}
}
的index.php
<?php
include_once('core/init.php');
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'email' => array('required' => true),
'password' => array('required' => true)
));
if($validation->passed()) {
$user = new User();
$login = $user->login(Input::get('email'), Input::get('password'));
if($login) {
echo 'Success';
} else {
echo '<p>Sorry, incorrect credentials</p>';
}
} else {
foreach($validation->errors() as $error) {
echo $error, '</br>';
}
}
}
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Test Page</title>
<link rel='shortcut icon' href='favicon.ico'/>
<link rel='stylesheet' type='text/css' href='res/css/bootstrap.css'/>
<link rel='stylesheet' type='text/css' href='res/css/jquery-ui.css'/>
<script type='text/javascript' src='res/js/jquery-ui.js'></script>
<script type='text/javascript' src='res/js/bootstrap.js'></script>
</head>
<body>
<nav id='navContainer'>
<div id='hIconWrapper'><img id='headerIcon' src='res/icon.png'></img></div>
</nav>
<div id='loginContainer'>
<div id='header'><span>Login</span></div>
<div id='inputContainer'>
<form method='post' action=''>
<label id='labelFormEmail'>Email</label></br><input type='email' name='email' placeholder='Email'/></br></br>
<label id='labelFormPassword'>Password</label></br><input type='password' name='password' placeholder='Password'/></br>
<a href='forgot.php' class='forgot'>Forgot ?</a>
<input type='hidden' name='token' value='<?php echo Token::generate(); ?>'/>
<button class='btn btn-default btn-primary' id='myBtn' type='submit' name='submit'>Login</button>
</form>
</div>
</div>
</body>
</html>
档案清单:
我尝试过的事情:
如果你们需要查看其他代码文件,请随时告诉我,以便我可以在此处添加。
答案 0 :(得分:0)
显然我的问题出现在User.php文件的第18行。它正在检查用户名列而不是EMAIL,这是我的数据库中存在的列。因此,基本上我所要做的就是将三元运算符更改为:
$field = (is_numeric($user)) ? 'id' : 'email';
做了一些测试,它的确有效!所以感谢所有尽力帮助我的人,尤其是Mark Baker指出这段代码:
$data = $this->_db->get('users', array($field, '=', $user));
user.php的
<?php
class User {
private $_db,
$_data;
public function __construct($user = null) {
$this->_db = DB::getInstance();
}
public function create($fields = array()){
if(!$this->_db->insert('users', $fields)) {
throw new Exception('There was a problem creating user');
}
}
public function find($user = null) {
if($user) {
$field = (is_numeric($user)) ? 'id' : 'username';
$data = $this->_db->get('users', array($field, '=', $user));
if($data->count()) {
$this->_data = $data->first();
return true;
}
}
return false;
}
public function login($username = null, $password = null) {
$user = $this->find($username);
if($user){
if(Hash::check($password, $this->data()->password)) {
Session::put($this->_sessionName, $this->data()->id);
return true;
}
}
return false;
}
private function data() {
return $this->_data;
}
}