Fatal:在 null 上调用成员函数 query()



我是PHP编程的新手,我正在创建一个接收查询的PHP类,但是当召唤时给我错误"PHP致命错误:在null上调用成员函数查询(("。 插入数据时。

class users {
public $host = "localhost";
public $username = "root";
public $password = "root1234";
public $db_name = "boot_quiz_oops";
public $conn;

public function _construct() {
$this->conn = new mysqli($this->host, $this->username, $this->password, $this->db_name);
if ($this->conn->connect_errno) {
die("database connection failed" . ($this->conn->connect_errno));
}
}
public function signup($data)
{
$this->conn->query($data);
return true;
}

这是我的第二个文件

<?php
include("class/users.php");
$register = new users;
extract($_POST);
$img_name = $_FILES['img']['name'];
$tmp_name = $_FILES['img']['tmp_name'];
move_uploaded_file($tmp_name, "img/".$img_name);
$query = "insert into signup values ('','$n','$e','$p','$img_name') ";
if($register->signup($query))
{
$register->url("index.php?run=success");
}
?>

这是我的索引.php文件

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Quiz</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br>
<div class="container">
<div class="row">
<br>            
<div class="col-sm-12">  
<div class="panel panel-primary">
<div class="panel-heading">Online Quiz Portal</div>
<div class="panel-body">Quiz in php</div>
</div>
</div>
</div>   
</div>
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="panel panel-info">
<div class="panel-heading"><h4>Login Form</h4></div>
<div class="panel-body">
<?php
if(isset($_GET['run']) && $_GET['run']=="failed")
{
echo "Your email and password does not match";
} 
?>     
<form role="form" action="signin_sub.php" method="post" >
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="e" id="email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="p" id="pwd" placeholder="Enter password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>
<div class="col-sm-6">    
<div class="panel panel-info">
<div class="panel-heading"><h4>Signup Form</h4></div>
<div class="panel-body">
<?php
if (isset($_GET['run']) && $_GET['run']=="success"){
echo "Your resgistration successfully done";
}
?>
<form role="form" action="signup_sub.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" name="n" id="name" placeholder="Enter name">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="e" id="email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="p" id="pwd" placeholder="Enter password">
</div>
<div class="form-group">
<label for="imgage">Upload your image:</label>
<input type="file" class="form-control" name="img" id="file">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>    
</div>
</div>        
</div>         
</div>
</body>
</html>

您的构造器名称错误:

你有_construct,但应该有__construct- 带有双下划线。

用户.php

需要改变

public function __construct() {
$this->conn = new mysqli($this->host, $this->username, $this->password, $this->db_name);
if ($this->conn->connect_errno) {
die("database connection failed" . ($this->conn->connect_errno));
}
}

将构造方法更改为:

public function __construct() {
$this->conn = new mysqli($this->host, $this->username, $this->password, $this->db_name);
if ($this->conn->connect_errno) {
die("database connection failed" . ($this->conn->connect_errno));
}
}

错误在_construct()中,它必须带有 2 个子行 __,就像这样__construct()

最新更新