Fatal: Calling a member function () function
I am new to PHP programming and I am creating a PHP class that receives requests, but when the callee gives me the error "PHP Fatal error: Call member function () on null". when inserting data.
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;
}
this is my second file
<?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");
}
?>
this is my index.php file
<!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>
+3
source to share
2 answers
Your constructor has the wrong name:
You have _construct
, but must have __construct
- double underscore.
user.php
Need to change
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
source to share
Change your build method as follows:
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));
}
}
The error is in _construct()
, it should be with two sublines __ like this__construct()
+1
source to share