HEX
Server: LiteSpeed
System: Linux server214.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User: bigisxfd (746)
PHP: 8.4.15
Disabled: NONE
Upload Files
File: /home/bigisxfd/public_html/cowork/confirm-online.php
<?php																																										

// Include necessary files and initialize database connection
include_once("functions.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
$bookingId    = $_GET['bookingId'];
$packageId    = $_GET['PackageId'];
$packagename  = $_GET['packagename'];
$memberId     = $_GET['memberid'];

// 1) Fetch the package details from Memberships
$packageQuery  = "SELECT * FROM Memberships WHERE packageid = ?";
$packageStmt   = $conn->prepare($packageQuery);
$packageStmt->bind_param("i", $packageId);
$packageStmt->execute();
$packageResult = $packageStmt->get_result();
$packageData   = $packageResult->fetch_assoc();
$packageStmt->close();

$durationDays   = $packageData['days'];
$durationHours  = $packageData['hours'];
$durationMonths = $packageData['months'];
$orderAmount    = $packageData['price']; // Adjust logic as needed

// 2) Calculate end date
//    Make sure $startDate is defined or if you want to use 'NOW()' as the start
$startDate = date('Y-m-d H:i:s'); // or fetch from the Bookings table if needed
$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"));

// 3) Update the booking record
$updateBookingQuery = "
    UPDATE Bookings 
    SET 
        active   = 1, 
        startdate = NOW(), 
        enddate   = ?
    WHERE bookingid = ?
";
$updateBookingStmt = $conn->prepare($updateBookingQuery);
$updateBookingStmt->bind_param("si", $endDate, $bookingId);

if (!$updateBookingStmt->execute()) {
    header("Location: confirm-online.php?error=query_execution");
    exit();
}
$updateBookingStmt->close();

// 4) Check if an order already exists for this booking ID
$checkOrderQuery = "
    SELECT orderid 
    FROM Orders 
    WHERE bookingid = ? 
    LIMIT 1
";
$checkOrderStmt = $conn->prepare($checkOrderQuery);
$checkOrderStmt->bind_param("i", $bookingId);
$checkOrderStmt->execute();
$checkOrderResult = $checkOrderStmt->get_result();
$checkOrderStmt->close();

// 5) If no existing order, insert a new one
if ($checkOrderResult->num_rows === 0) {
    // Insert order into Orders table with bookingid
    $insertOrderQuery = "
        INSERT INTO Orders (
            payment_status,
            memberid,
            userid,
            bookingid,
            date,
            amount,
            items
        ) VALUES (
            0,     -- payment_status
            ?,     -- memberid
            ?,     -- userid
            ?,     -- bookingid
            NOW(), -- date
            ?,     -- amount
            ?
        )
    ";
    $orderStmt = $conn->prepare($insertOrderQuery);
    if (!$orderStmt) {
        die("Error during order query preparation: " . $conn->error);
    }

    // Bind parameters
    // i = integer, s = string (or 'd' if your amount is decimal), etc.
    $orderStmt->bind_param("iiiss", $memberId, $userId, $bookingId, $orderAmount, $packagename);

    if (!$orderStmt->execute()) {
        die("Error during order insertion: " . $orderStmt->error);
    }
    $orderStmt->close();
}

// 6) Redirect to success page
header("Location: bookings.php?bookingId=" . $bookingId);
exit();
?>