34566-vm/create_booking.php
2025-10-03 00:08:51 +00:00

43 lines
1.3 KiB
PHP

<?php
header('Content-Type: application/json');
require_once 'db/config.php';
$response = ['status' => 'error', 'message' => 'Invalid request'];
$data = json_decode(file_get_contents('php://input'), true);
if ($data) {
try {
$pdo = db();
// Main booking
$stmt = $pdo->prepare("INSERT INTO bookings (title, sport, start_time, end_time) VALUES (?, ?, ?, ?)");
$stmt->execute([
$data['title'],
$data['sport'],
$data['start'],
$data['end']
]);
// Buffer booking
$buffer_start = $data['end'];
$buffer_end_dt = new DateTime($buffer_start);
$buffer_end_dt->modify('+15 minutes');
$buffer_end = $buffer_end_dt->format('Y-m-d H:i:s');
$stmt_buffer = $pdo->prepare("INSERT INTO bookings (title, sport, start_time, end_time) VALUES (?, ?, ?, ?)");
$stmt_buffer->execute([
'Buffer',
'Buffer',
$buffer_start,
$buffer_end
]);
$response['status'] = 'success';
$response['message'] = 'Booking created successfully';
} catch (Exception $e) { // Catch generic Exception
$response['message'] = 'Database error: ' . $e->getMessage();
}
}
echo json_encode($response);
?>