Encountering an error while executing a PHP database insert. It mentions a non-object property in a class file. How can this issue be resolved?
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require('dbconfig.php');
require('DataHandler.php');
$userEmail = filter_input(INPUT_POST, 'userEmail', FILTER_SANITIZE_EMAIL);
$commCode = filter_input(INPUT_POST, 'commCode', FILTER_SANITIZE_STRING);
$groupName = filter_input(INPUT_POST, 'groupName', FILTER_SANITIZE_STRING);
$handler = new DataHandler();
$handler->connect();
$insertStatus = $handler->addGroup($userEmail, $commCode, $groupName);
$handler->disconnect();
?>
<?php
class DataHandler {
private $connection;
public function connect() {
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($this->connection->connect_error) {
throw new Exception('Connection error: ' . $this->connection->connect_error);
}
}
public function disconnect() {
if ($this->connection) {
$this->connection->close();
}
}
public function addGroup($email, $code, $name) {
$stmt = $this->connection->prepare('INSERT INTO groups (user_email, comm_code, group_name) VALUES (?, ?, ?)');
if (!$stmt) {
throw new Exception('Preparation failed: ' . $this->connection->error);
}
$stmt->bind_param('sss', $email, $code, $name);
return $stmt->execute();
}
}
?>