register.php
<?php
require_once 'core/init.php';
if(Input::exists())
{
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users'
),
'password' => array(
'required' => true,
'min' => 6,
),
'password_again' => array(
'required' => true,
'matches' => 'password'
),
'name' => array(
'required' => true,
'min' => 2,
'max' => 50
),
));
if($validation->passed())
{
echo "Passed";
}
else
{
foreach ($validation->errors() as $error)
{
echo $error, '<br>';
}
}
}
?>
<form action="" method="post">
<div class="field">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')) ?>" autocomplete="off">
</div>
<div class="field">
<label for="password">Choose a password</label>
<input type="password" name="password" id="password" value="<?php echo escape(Input::get('password')) ?>" autocomplete="off">
</div>
<div class="field">
<label for="password_again">Enter your password again</label>
<input type="password" name="password_again" id="password_again" value="<?php echo escape(Input::get('password_again')) ?>" autocomplete="off">
</div>
<div class="field">
<label for="name">Your name</label>
<input type="text" name="name" id="name" value="<?php echo escape(Input::get('name')) ?>" autocomplete="off">
</div>
<input type="submit" name="register">
</form>
db.php中
<?php
class db
{
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct()
{
try
{
$this->_pdo = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
public static function getInstance()
{
if(!isset(self::$_instance))
{
self::$_instance = new db();
}
return self::$_instance;
}
public function query($sql, $params = array())
{
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql))
{
$x = 1;
if(count($params))
{
foreach($params as $param)
{
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute())
{
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}
else
{
$this->_error = true;
}
}
return $this;
}
public function action($action, $table, $where = array())
{
if(count($where) === 3)
{
$operators = array('=', '>', '<', '>=', '<=');
$field = '$where[0]';
$operator = '$where[1]';
$value = '$where[2]';
if(in_array($operator, $operators))
{
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error())
{
return $this;
}
}
}
return false;
}
public function get($table, $where)
{
return $this->action('SELECT * ', $table, $where);
}
public function delete($table, $where)
{
return $this->action('DELETE', $table, $where);
}
public function insert($table, $fields = array())
{
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach($fields as $field)
{
$values .= '?';
if($x < count($fields))
{
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO users (`".implode('`, `', $keys)."`) VALUES ({$values})";
if(!$this->query($sql, $fields)->error())
{
return true;
}
return false;
}
public function update($table, $id, $fields)
{
$set = '';
$x = 1;
foreach($fields as $name => $value)
{
$set .= "{$name} = ?";
if($x < count($fields))
{
$set .= ', ';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";
if(!$this->query($sql, $fields)->error())
{
return true;
}
return false;
}
public function results()
{
return $this->_results;
}
public function error()
{
return $this->_error;
}
public function count()
{
return $this->_count;
}
}
validate.php
<?php
class Validate
{
private $_passed = false,
$_errors = array(),
$_db = null;
public function __construct()
{
$this->_db = db::getInstance();
}
public function check($source, $items = array())
{
foreach($items as $item => $rules)
{
foreach($rules as $rule => $rule_value)
{
$value = trim($source[$item]);
$item = escape($item);
if($rule === 'required' && empty($value))
{
$this->addError("{$item} is required");
}
else if(!empty($value))
{
switch($rule)
{
case 'min':
if(strlen($value) < $rule_value)
{
$this->addError("{$item} must be a minimum of {$rule_value} characters");
}
break;
case 'max':
if(strlen($value) > $rule_value)
{
$this->addError("{$item} must be a maximum of {$rule_value} characters");
}
break;
case 'matches':
if($value != $source[$rule_value])
{
$this->addError("{$rule_value} must match {$item}");
}
break;
case 'unique':
$check = $this->_db->get($rule_value, array($item, '=', $value));
if($this->count())
{
$this->addError("{$item} already exists");
}
break;
}
}
}
}
if(empty($this->_errors))
{
$this->passed = true;
}
return $this;
}
private function addError($error)
{
$this->_errors[] = $error;
}
public function errors()
{
return $this->_errors;
}
public function passed()
{
return $this->_passed;
}
}
的init.php
<?php
session_start();
$GLOBALS['config'] = array
(
'mysql' => array
(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => 'lr'
),
'remember' => array
(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array
(
'session_name' => 'user'
)
);
spl_autoload_register(function($class)
{
require_once 'classes/'.$class.'.php';
});
register.php包含注册表单。我还没有编写代码将其存储在数据库中,我只是对输入进行了验证。 register.php返回错误说明未定义的方法count()。确切的错误是
致命错误:未捕获错误:调用未定义的方法在C:\ xampp \ htdocs \ LoginRegistrationSystem \ classes \ validate.php中验证:: count():50堆栈跟踪:#0 C:\ xampp \ htdocs \ LoginRegistrationSystem \ register.php(8):在第50行的C:\ xampp \ htdocs \ LoginRegistrationSystem \ classes \ validate.php中抛出的Validate-&gt; check(Array,Array)#1 {main}
请帮助,我通过代码3-4次,但无法找到错误。谢谢!
答案 0 :(得分:0)
Imho你还没有在Validate类中实现函数count()(你使用if($ this-&gt; count())在Validate.php的第50行调用它。)
实现该功能或更改调用它的功能应来自不同的类。 ($ this-&gt; _db-&gt; count()最有可能)