I was in the process of developing an RESTFul API for my recent project after creating a
class and testing it in postman it displays the following error
Error displayed in postman
<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function bind_param() on bool in
C:\xampp\htdocs\phpRestfulAPI\sample1\classes\students.php:35
Stack trace:
#0 C:\xampp\htdocs\phpRestfulAPI\sample1\v1\create.php(21): Student->create_data()
#1 {main}
thrown in <b>C:\xampp\htdocs\phpRestfulAPI\sample1\classes\students.php</b> on line <b>35</b><br />
Class code
<?php
class Student
{
//Declare variables
public $name;
public $email;
public $mobile;
private $conn;
private $table_name;
//Constructor
public function __construct($db)
{
$this->conn = $db;
$this->table_name = "tbistudent";
}
public function create_data()
{
//SQL query to insert data
$query = "INSERT INTO" . $this->table_name . "SET name= ?, email = ?, mobile = ?";
//Prepare the sql
$obj = $this->conn->prepare($query);
//Sanitize input variables => basically removes extra characters like some special
//symbols as wel as if some tags available in input values
$this->name = htmlspecialchars(strip_tags($this->name));
$this->email = htmlspecialchars(strip_tags($this->email));
$this->mobile = htmlspecialchars(strip_tags($this->mobile));
//Bind parameters with prepared statement
$obj->bind_param("sss", $this->name, $this->email, $this->mobile);
//$obj->store_result();
if ($obj->execute()) {
//Execute query
return true;
} else {
# code...
return false;
}
}
}
Regards