我有个问题。我需要从我的数据库类获取 mysqli 连接以注册类。但它不起作用。我总是收到如下错误:
Call to undefined method Database::query() in
C:\xampp\htdocs\registration\class_register.php on line 47
我可能需要链接该连接..但我不知道该怎么做。我发现了一些关于那个的话题..但没有任何效果。感谢帮助 :)
class_register.php
<?php
include "class_db.php";
class Register
{
private $username;
private $password;
private $passmd5;
private $email;
private $errors;
private $token;
private $link_mysqli;
public function __construct()
{
$this->errors = array();
$this->username = $this->filter($_POST['uname']);
$this->password = $this->filter($_POST['upass']);
$this->email = $this->filter($_POST['umail']);
$this->token = $_POST['token'];
$this->passmd5 = md5($this->password);
$this->link_mysqli = new Database();//instance of Database Class
}
public function process()
{
if($this->valid_token() && $this->valid_data())
$this->register();
return count($this->errors) ? 0 : 1;
}
public function filter($var)
{
return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
}
public function register()
{
$result = $this->link_mysqli->query("INSERT INTO users (username,password) VALUES ('$this->username', '$this->passmd5')");
if($result->link_mysqli->affected_rows() < 1)
$this->errors[] = 'Nemožno vytvoriť účet';
}
public function show_errors()
{
echo "<h3>Errors</h3>";
foreach($this->errors as $key=>$value)
echo $value.'<br>';
}
public function valid_data()
{
if(empty($this->username))
$this->errors[] = 'Nesprávne uživateľské meno';
if(empty($this->password))
$this->errors[] = 'Nesprávne uživateľské heslo';
if(empty($this->email) || !filter_var($this->email, FILTER_VALIDATE_EMAIL))
$this->errors[] = 'Nesprávny e-mail';
return count($this->errors) ? 0 : 1;
}
public function valid_token()
{
if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
$this->errors[] = 'Nesprávna Relácia';
return count($this->errors) ? 0 : 1;
}
function status() {
return print_r($this,true);
}
}
?>
class_db.php
<?php
class Database
{
private $mysqli;
private $db_host = "127.0.0.1";
private $db_user = "register";
private $db_pass = "159753";
private $db_database = "registration";
public function __construct()
{
$this->mysqli = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_database);
if(mysqli_connect_errno()){
printf("Nastala chyba pripojenia s hláškou: %s", $mysqli->errno(), $mysqli->error());
die();
}
return $this->mysqli;
}
public function __destruct()
{
$this->mysqli->close();
}
}
?>
索引.php
<?php
session_start();
if(isset($_POST['register'])){
include "class_register.php";
$register = new Register();
if($register->process())
echo "Úspešne zaregistrovaný";
else
$register->show_errors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td><label for="uname">Meno:</label></td>
<td><input type="text" id="uname" name="uname" /></td>
</tr>
<tr>
<td><label for="upass">Heslo:</label></td>
<td><input type="password" id="upass" name="upass" /></td>
</tr>
<tr>
<td><label for="umail">E-Mail:</label></td>
<td><input type="text" id="umail" name="umail" /></td>
</tr>
<input type="hidden" name="token" value="<?php echo $token; ?>" />
<tr><td><input type="submit" name="register" value="Registrovať" /></td></tr>
</table>
</form>