36716-vm/add-availability.php
2025-12-07 05:00:42 +00:00

49 lines
1.5 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'coach') {
// Redirect non-coaches or non-logged-in users
header('Location: login.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$coach_id = $_SESSION['user_id'];
$date = $_POST['date'];
$start_time = $_POST['start_time'];
$end_time = $_POST['end_time'];
// Validation (basic)
if (empty($date) || empty($start_time) || empty($end_time)) {
header('Location: dashboard.php?status=error&message=All+fields+are+required');
exit;
}
$start_datetime = $date . ' ' . $start_time;
$end_datetime = $date . ' ' . $end_time;
// Check if end time is after start time
if (strtotime($start_datetime) >= strtotime($end_datetime)) {
header('Location: dashboard.php?status=error&message=End+time+must+be+after+start+time');
exit;
}
try {
$stmt = db()->prepare("INSERT INTO coach_availability (coach_id, start_time, end_time) VALUES (?, ?, ?)");
$stmt->execute([$coach_id, $start_datetime, $end_datetime]);
header('Location: dashboard.php?status=success&message=Availability+added+successfully');
exit;
} catch (PDOException $e) {
// Log error and redirect
error_log('Error adding availability: ' . $e->getMessage());
header('Location: dashboard.php?status=error&message=Could+not+add+availability');
exit;
}
}
// Redirect if accessed directly without POST
header('Location: dashboard.php');
exit;