I am making a timesheet system as a class project, and I have stumbled into some issues with the admin_class.php and ajax.php files. When I submit what I want to submit it gives me this error as a response:
Fatal error: Uncaught Error: Call to a member function real_escape_string() on null in C:xampphtdocsrbaaadmin_class.php:430 Stack trace: #0 C:xampphtdocsrbaaajax.php(160): Action->save_timesheet(Array) #1 {main} thrown in C:xampphtdocsrbaaadmin_class.php on line 430
ajax.php:
if ($action == 'save_timesheet') {
require_once 'admin_class.php';
$admin = new Action(); // Change Admin to Action
// Receive data from form
$data = [
'id' => $_POST['id'],
'client_id' => $_POST['client_id'],
'audit_members' => $_POST['audit_members'],
'date_started' => $_POST['date_started'],
'start_time' => $_POST['start_time'],
'end_time' => $_POST['end_time']
];
$response = $admin->save_timesheet($data);
echo $response; // Return success or error message
}
admin_class.php:
public function save_timesheet($data) {
global $conn;
$client_id = intval($data['client_id']);
$audit_members = implode(',', array_map('intval', $data['audit_members']));
$date_started = $conn->real_escape_string($data['date_started']);
$start_times = json_encode($data['start_time']);
$end_times = json_encode($data['end_time']);
// Insert into timesheets table
$sql = "INSERT INTO timesheets (client_id, audit_members, date_started, start_time, end_time)
VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("iisss", $client_id, $audit_members, $date_started, $start_times, $end_times);
if ($stmt->execute()) {
return 1; // Success
} else {
return $stmt->error; // Error
}
}