RoamEase/indexes/user/process/modify/item_subtract.php
2025-06-06 17:31:03 +08:00

69 lines
2.0 KiB
PHP

<?php
include "../../../ConnectDB.php";
session_start();
$uid = mysqli_real_escape_string($conn, $_SESSION['uid']);
if (isset($_GET["booking_id"]) && isset($_GET["table"])) {
$booking_id = $_GET["booking_id"];
switch ($_GET["table"]) {
case '1':
$table = "hotel_booking";
break;
case '2':
$table = "restaurant_booking";
break;
case '3':
$table = "attraction_booking";
break;
case '4':
$table = "guide_booking";
break;
case '5':
$table = "transport_flight_booking";
break;
case '6':
$table = "transport_railway_booking";
break;
default:
echo "Unknown booking type";
}
$price_query = "SELECT `subtotal_price` FROM cart_bookings WHERE booking_id=" . $booking_id;
$priceRes = mysqli_query($conn, $price_query);
$price = mysqli_fetch_assoc($priceRes);
$price = $price["subtotal_price"];
$quantity_query = "SELECT quantity FROM `$table` WHERE booking_id='$booking_id'";
$res = mysqli_query($conn, $quantity_query);
$quantity = mysqli_fetch_assoc($res);
$quantity = $quantity['quantity'];
if($quantity > 1){
$add = "UPDATE `$table` SET quantity = '$quantity' - 1 WHERE booking_id='$booking_id'";
$res = mysqli_query($conn, $add);
if (!$res) {
echo "<script type='text/javascript'>
alert('Error occurred!');
</script>";
}
$total = "UPDATE user_buyer SET cart_total_price = cart_total_price - '$price' WHERE u_id='$uid'";
$res = mysqli_query($conn, $total);
if (!$res) {
echo "<script type='text/javascript'>
alert('Error occurred!');
</script>";
}
}
else{
echo "<script type='text/javascript'>
alert('You cannot buy 0 quantity!');
</script>";
}
}
header('Location: ../cart.php');
?>