TKK_E32230273/save_room_restriction.php

32 lines
1.1 KiB
PHP

<?php
ob_start();
include 'koneksi.php';
$output = ob_get_clean(); // Prevent whitespace corruption
header('Content-Type: application/json');
$day = $_POST['day_of_week'] ?? '';
$timeFrom = $_POST['time_from'] ?? '';
$timeTo = $_POST['time_to'] ?? '';
if (empty($day) || empty($timeFrom) || empty($timeTo)) {
echo json_encode(['status' => 'error', 'message' => 'All fields are required.']);
exit;
}
// Append seconds to satisfy strict MySQL TIME column mode
if (strlen($timeFrom) === 5) $timeFrom .= ':00';
if (strlen($timeTo) === 5) $timeTo .= ':00';
try {
$stmt = $pdo->prepare("INSERT INTO room_restrictions (day_of_week, time_from, time_to) VALUES (?, ?, ?)");
$result = $stmt->execute([$day, $timeFrom, $timeTo]);
if ($result) {
echo json_encode(['status' => 'success', 'message' => 'Restriction saved successfully.']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to save: ' . implode(' ', $stmt->errorInfo())]);
}
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Exception: ' . $e->getMessage()]);
}