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

时间:2017-07-04 08:47:07

标签: php mysqli

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

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");
}
?>

这是我的index.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>

2 个答案:

答案 0 :(得分:1)

您的构造函数名称错误:

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

user.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));
    }
}

答案 1 :(得分:1)

将构造方法更改为:

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()