window.top.location.href = '../../login/username.php?error=You need to login to process this action!'; "; } if (isset($_GET["target_id"]) && isset($_GET["start_date"]) && isset($_GET["end_date"]) && isset($_GET["quantity"]) && isset($_GET["table"])) { $target_id = $_GET["target_id"]; $start_date = $_GET["start_date"]; $end_date = $_GET["end_date"]; if ($end_date && $end_date !== 'NULL'){ // Convert the dates to DateTime objects $start = new DateTime($start_date); $end = new DateTime($end_date); // Calculate the difference $interval = $start->diff($end); $days = $interval->days; } $quantity = intval($_GET["quantity"]); $new_booking_query = "INSERT INTO `bookings` (`booking_id`) VALUES (NULL)"; $new_booking_set = mysqli_query($conn, $new_booking_query); $booking_id = mysqli_insert_id($conn); switch ($_GET["table"]) { case '1': $price_query = "SELECT price FROM hotel_room WHERE hotel_room_id=" . $target_id; $priceRes = mysqli_query($conn, $price_query); $price = mysqli_fetch_assoc($priceRes); $price = $price["price"]; $subtotal_price = $price * $days; $query = "INSERT INTO `hotel_booking` (`booking_id`, `hotel_room_id`, `start_date`, `end_date`, `quantity`, `payment_status`) VALUES ('$booking_id', '$target_id', '$start_date', '$end_date', '$quantity', '0')"; $cart = "INSERT INTO `cart_bookings` (`u_id`, `booking_id`, `booking_type_id`, `subtotal_price`, `choose`) VALUES ('$uid', '$booking_id', '1', '$subtotal_price', '1')"; break; case '2': $price_query = "SELECT fee FROM restaurant WHERE restaurant_id=" . $target_id; $priceRes = mysqli_query($conn, $price_query); $price = mysqli_fetch_assoc($priceRes); $price = $price["fee"]; $subtotal_price = $price; $query = "INSERT INTO `restaurant_booking` (`booking_id`, `restaurant_id`, `visit_date`, `quantity`, `payment_status`) VALUES ('$booking_id', '$target_id', '$start_date', '$quantity', '0')"; $cart = "INSERT INTO `cart_bookings` (`u_id`, `booking_id`, `booking_type_id`, `subtotal_price`, `choose`) VALUES ('$uid', '$booking_id', '2', '$subtotal_price', '1')"; break; case '3': $price_query = "SELECT price FROM attraction WHERE attraction_id=" . $target_id; $priceRes = mysqli_query($conn, $price_query); $price = mysqli_fetch_assoc($priceRes); $price = $price["price"]; $subtotal_price = $price; $query = "INSERT INTO `attraction_booking` (`booking_id`, `attraction_id`, `visit_date`, `quantity`, `payment_status`) VALUES ('$booking_id', '$target_id', '$start_date', '$quantity', '0')"; $cart = "INSERT INTO `cart_bookings` (`u_id`, `booking_id`, `booking_type_id`, `subtotal_price`, `choose`) VALUES ('$uid', '$booking_id', '3', '$subtotal_price', '1')"; break; case '4': $price_query = "SELECT fee FROM user_guide WHERE u_id=" . $target_id; $priceRes = mysqli_query($conn, $price_query); $price = mysqli_fetch_assoc($priceRes); $price = $price["fee"]; $subtotal_price = $price * $days; $query = "INSERT INTO `guide_booking` (`booking_id`, `guide_id`, `start_date`, `end_date`, `quantity`, `guide_availability`, `payment_status`) VALUES ('$booking_id', '$target_id', '$start_date', '$end_date', '$quantity', '0', '0')"; $cart = "INSERT INTO `cart_bookings` (`u_id`, `booking_id`, `booking_type_id`, `subtotal_price`, `choose`) VALUES ('$uid', '$booking_id', '4', '$subtotal_price', '1')"; break; case '5': $price_query = "SELECT fee FROM transport_flight_class WHERE flight_class_id=" . $target_id; $priceRes = mysqli_query($conn, $price_query); $price = mysqli_fetch_assoc($priceRes); $price = $price["fee"]; $subtotal_price = $price; $query = "INSERT INTO `transport_flight_booking` (`booking_id`, `flight_class_id`, `visit_date`, `quantity`, `payment_status`) VALUES ('$booking_id', '$target_id', '$start_date', '$quantity', '0')"; $cart = "INSERT INTO `cart_bookings` (`u_id`, `booking_id`, `booking_type_id`, `subtotal_price`, `choose`) VALUES ('$uid', '$booking_id', '5', '$subtotal_price', '1')"; break; case '6': $price_query = "SELECT fee FROM transport_railway_class WHERE train_seat_id=" . $target_id; $priceRes = mysqli_query($conn, $price_query); $price = mysqli_fetch_assoc($priceRes); $price = $price["fee"]; $subtotal_price = $price; $query = "INSERT INTO `transport_railway_booking` (`booking_id`, `railway_class_id`, `visit_date`, `quantity`, `payment_status`) VALUES ('$booking_id', '$target_id', '$start_date', '$quantity', '0')"; $cart = "INSERT INTO `cart_bookings` (`u_id`, `booking_id`, `booking_type_id`, `subtotal_price`, `choose`) VALUES ('$uid', '$booking_id', '6', '$subtotal_price', '1')"; break; default: echo "Unknown booking type"; break; } $firstQueryResult = mysqli_query($conn, $query); if ($firstQueryResult) { $cartQueryResult = mysqli_query($conn, $cart); if (!$cartQueryResult) { // Log error or display a more specific message $errorMsg = mysqli_error($conn); echo "

Error occurred while executing cart query: $errorMsg

"; } } else { // Log error or display a more specific message $errorMsg = mysqli_error($conn); echo "

Error occurred while executing the first query: $errorMsg

"; } $total = "UPDATE user_buyer SET cart_total_price = cart_total_price + ('$subtotal_price' * '$quantity') WHERE u_id='$uid'"; $res = mysqli_query($conn, $total); if (!$res) { echo ""; } } header('Location: ./process/cart.php'); ?>