File: /home/bigisxfd/public_html/cowork/process_booking.php
<?php
// Include necessary files and initialize database connection
include_once("functions.php");
// Start the session (assuming you have already started the session in your header.php)
session_start();
// Check if the user is logged in
if (!isset($_SESSION["loggedin"])) {
header("Location: login.php");
exit(); // Stop script execution here
}
// Get user ID from the session
$userId = $_SESSION['userid'];
// Get form data
$memberId = $_POST['member'];
$packageId = $_POST['package'];
// Check if the member already has an active booking
$activeBookingQuery = "SELECT * FROM Bookings WHERE memberid = ? AND active = 1";
$activeBookingStmt = $conn->prepare($activeBookingQuery);
$activeBookingStmt->bind_param("i", $memberId);
$activeBookingStmt->execute();
$activeBookingResult = $activeBookingStmt->get_result();
if ($activeBookingResult->num_rows > 0) {
header("Location: pos.php?error=active_booking");
exit(); // Stop script execution here
}
// Fetch package details
$query = "SELECT * FROM Memberships WHERE packageid = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $packageId);
$stmt->execute();
$result = $stmt->get_result();
$package = $result->fetch_assoc();
// Calculate end time based on package duration
$bookingDate = date('Y-m-d H:i:s'); // Current date and time
$startDate = $bookingDate;
$durationDays = $package['days'];
$durationHours = $package['hours'];
$durationMonths = $package['months'];
// Calculate the total seconds for each duration
$totalSeconds = ($durationDays * 24 * 60 * 60) + ($durationHours * 60 * 60) + ($durationMonths * 30 * 24 * 60 * 60);
$endDate = date('Y-m-d H:i:s', strtotime($startDate . " +$totalSeconds seconds"));
// Insert booking into Bookings table
$bookingQuery = "INSERT INTO Bookings (memberid, startdate, enddate, packageid, login, active) VALUES (?, ?, ?, ?, ?, 1)";
$bookingStmt = $conn->prepare($bookingQuery);
if (!$bookingStmt) {
header("Location: pos.php?error=query_preparation");
exit(); // Stop script execution here
}
$bookingStmt->bind_param("issss", $memberId, $startDate, $endDate, $packageId, $startDate);
if (!$bookingStmt->execute()) {
header("Location: pos.php?error=query_execution");
exit(); // Stop script execution here
}
$bookingId = mysqli_insert_id($conn); // Get the last inserted booking ID
$bookingStmt->close();
// Insert order into Orders table
$orderAmount = $package['price']; // You might need to adjust this based on your logic
// Get the packagename
$packagename = $package['packagename'];
$insertOrderQuery = "INSERT INTO Orders (payment_status, memberid, userid, date, amount, items) VALUES (0, ?, ?, NOW(), ?, ?)";
$orderStmt = $conn->prepare($insertOrderQuery);
$orderStmt->bind_param("iiss", $memberId, $userId, $orderAmount, $packagename);
if (!$orderStmt) {
die("Error during order query preparation: " . $conn->error);
}
if (!$orderStmt->execute()) {
die("Error during order insertion: " . $orderStmt->error);
}
$orderStmt->close();
// Redirect to success page
header("Location: bookings.php?success=true");
exit();
?>
?>