gpt4 book ai didi

php - OOP 登录/注册

转载 作者:行者123 更新时间:2023-11-30 22:28:10 26 4
gpt4 key购买 nike

我是 PHP 的新手,我正在为一个小类(class)做 OOP 登录/注册教程,所以它不适用于 www.一小时或 15 小时后,我卡在了一段似乎不起作用的代码上。我试图与我的一个 friend 一起解决这个问题,他对 PHP 有更多的了解,我搜索了谷歌,检查了多个论坛和所有 YouTube 评论以寻找解决方案,但我被卡住了。我知道所有其他代码都是正确的,因为到目前为止它都运行良好:

所以我的问题是:使独特案例起作用的正确 PHP 代码是什么。

            case 'unique'     
$check =$this->_db->get($rule_value,array($item,"=",
$value));
line 40> if($check->count()){
$this->addError("{$item} bestaat al.");

with this error:

! ) Fatal error: Call to a member function count() on a non-object in C:\wamp\www\websitegroop\classes\validate.php on line 40 调用堆栈

验证类:

class Validate  {
private $_passed = false,
$_errors = array(),
$_db = null;
public function __construct() {
$this->_db = Database::getInstance();
}

public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {

$value = trim($source[$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} moet minimaal {$rule_value} letters of cijfers zijn.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$item} kan maximaal {$rule_value} letters of cijfers zijn.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$rule_value} moet hetzelfde zijn als {$item}.");
}
break;
case 'unique':
$check = $this->_db->get($rule_value, array($item, "=", $value));
if($check->count()){
$this->addError("{$item} bestaat al.");
}
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;
}

}

注册.php:

               <?php
require_once 'core/init.php';

if(Input::exists()) {
if(Token::check(Input::get('token'))) {

$validate = new Validate();
$validation = $validate->check($_POST, array(
'gebruikersnaam' => array(
'required' => true,
'min' => 3,
'max' => 30,
'unique' => 'groopklanten'
),
'paswoord' => array(
'required' => true,
'min' => 6,
),
'paswoord_nogmaals' => array(
'required' => true,
'matches' => 'paswoord'
),
'bedrijfsnaam' => array(
'required' => true,
'min' => 3,
'max' => 30,
),
'kvknr' => array(
'required' => true,
'min' => 8,
'max' => 8,
'unique' => 'groopklanten'
),
'contactpersoon' => array(
'required' => true,
'min' => 4,
'max' => 30,
),
'functie' => array(
'required' => true,
'min' => 4,
'max' =>30,
),
'telbedrijf' => array(
'required' => true,
'min' => 10,
'max' => 12,
'unique' => 'groopklanten'
),
'adres' => array(
'required' => true,
'min' => 4,
'max' => 40,
'unique' => 'groopklanten'
),
'afleveradres' => array(
'min' => 4,
'max' => 40,
),
'postcode' => array(
'required' => true,
'min' => 6,
'max' => 7,
),
'woonplaats' => array(
'required' => true,
'min' => 3,
'max' => 30,
),
'emailadres' => array(
'required' => true,
'min' => 5,
'max' => 30,
'unique' => 'groopklanten'
)
));

if($validate->passed()) {
echo 'geregistreerd';
} else {
foreach($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
}

?>

<form action="" method="post">
<div class="field">
<label for="gebruikersnaam">Gebruikersnaam:</label>
<input type="text" name="gebruikersnaam" id="gebruikersnaam">
</div>

<div class="field">
<label for="paswoord">Kies een paswoord:</label>
<input type="password" name="paswoord" id="paswoord">
</div>

<div class="field">
<label for="paswoord_nogmaals">Nogmaals uw paswoord:</label>
<input type="password" name="paswoord_nogmaals" id="paswoord_nogmaals">
</div>

<div class="field">
<label for="bedrijfsnaam">Bedrijfsnaam:</label>
<input type="text" name="bedrijfsnaam" id="bedrijfsnaam">
</div>

<div class="field">
<label for="kvknr">KvK nummer:</label>
<input type="text" name="kvknr" id="kvknr">
</div>

<div class="field">
<label for="contactpersoon">Naam contactpersoon:</label>
<input type="text" name="contactpersoon" id="contactpersoon">
</div>

<div class="field">
<label for="functie">Functie:</label>
<input type="text" name="functie" id="functie">
</div>

<div class="field">
<label for="telbedrijf">Telefoonnummer bedrijf:</label>
<input type="tell" name="telbedrijf" id="telbedrijf">
</div>

<div class="field">
<label for="adres">Adres:</label>
<input type="text" name="adres" id="adres">
</div>

<div class="field">
<label for="afleveradres"> Afwijkend aflever adres:</label>
<input type="text" name="afleveradres" id="afleveradres">
</div>

<div class="field">
<label for="postcode">Postcode:</label>
<input type="text" name="postcode" id="postcode">
</div>

<div class="field">
<label for="woonplaats">Woonplaats:</label>
<input type="text" name="woonplaats" id="woonplaats">
</div>

<div class="field">
<label for="emailadres">E-mail adres:</label>
<input type="email" name="emailadres" id="emailadres">
</div>

<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<input type ="submit" value="Register">
<input type="reset" name="reset" value="Reset Velden">

<p> Bij het verzenden gaat u automatisch akoord met de <a href="algemenevoorwaarden.html"> algemene voorwaarden!</a></p>

输入类:

        <?php
class input {
public static function exists($type = 'post') {
switch($type) {
case 'post':
return (!empty($_POST)) ? true : false;
break;
case 'get':
return (!empty($_GET)) ? true : false;
break;
default:
return false;
break;
}
}

public static function get($item) {
if(isset($_POST[$item])) {
return $_POST[$item];
} else if(isset($_GET[$item])) {
return $_GET[$item];
}
return '';
}
}

数据库类:

       <?php
class Database {
private static $_instance = null;
private $_pdo,
$_qeury,
$_error = false,
$_result,
$_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 Database();
}
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} {value} ?";

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()) {
if(count($fields)) {
$keys = array_keys($fields);
$values = null;
$x = 1;

foreach($fields as $field) {
$values .= '?';
if ($x < count($fields)) {
$values .= ', ';
}
$x++;
}

$sql = "INSERT INTO groopklanten (`" . 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 first() {
return $this->_results[0];
}

public function error() {
return $this->_error;
}

public function count() {
return $this->_count;
}
}

初始化文件

         <?php
session_start();

$GLOBALS['config'] = array(
'mysql' => array(
'host' => 'localhost',
'username' => 'nick16061983',
'password' => 'N1sn0p1!A',
'db' => 'websitegroop'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'groopklanten',
'token_name' => 'token'

)
);

spl_autoload_register(function($class) {
require_once 'classes/' .$class. '.php';
});

到目前为止我尝试过的所有其他事情都告诉我,我总是可以注册一个现有的用户名,否则它总是会说它已经注册了。当用户名不在我的数据库中时。

提前致谢

尼克

最佳答案

Fatal error: Call to a member function count() on a non-object in C:\wamp\www\websitegroop\classes\validate.php on line 40 Call Stack

那是因为 action() 方法返回的是 false 而不是当前对象,这意味着您的查询执行失败。这主要有两个原因:

首先,请看下面的代码片段。这里有一个小语法错误,

class Database {
private static $_instance = null;
private $_pdo,
$_qeury, // syntax error

...

应该是,

class Database {
private static $_instance = null;
private $_pdo,
$_query,

...

其次,你的声明 $sql = "{$action} FROM {$table} WHERE {$field} {$operator} {value} ?"; inside action() 方法不对,应该是这样的:

$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

所以你的整个 Database 类应该是这样的:

<?php

class Database {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_result,
$_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 Database();
}
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()) {
if(count($fields)) {
$keys = array_keys($fields);
$values = null;
$x = 1;

foreach($fields as $field) {
$values .= '?';
if ($x < count($fields)) {
$values .= ', ';
}
$x++;
}

$sql = "INSERT INTO groopklanten (`" . 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 first() {
return $this->_results[0];
}

public function error() {
return $this->_error;
}

public function count() {
return $this->_count;
}
}

?>

关于php - OOP 登录/注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com