-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancel_order.php
More file actions
51 lines (42 loc) · 1.3 KB
/
cancel_order.php
File metadata and controls
51 lines (42 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
session_start();
include 'db.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
// Validate order ID
if (!isset($_POST['order_id']) || !is_numeric($_POST['order_id'])) {
echo "Invalid order ID.";
exit();
}
$order_id = $_POST['order_id'];
// Fetch the order details
$query = "SELECT * FROM orders WHERE id = ? AND status = 'Pending'";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $order_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
echo "Order cannot be cancelled.";
exit();
}
$order = $result->fetch_assoc();
// Restore stock if applicable
if (isset($order['product_id']) && isset($order['quantity'])) {
$update_stock_sql = "UPDATE products SET quantity = quantity + ? WHERE id = ?";
$stmt = $conn->prepare($update_stock_sql);
$stmt->bind_param("ii", $order['quantity'], $order['product_id']);
$stmt->execute();
}
// Update order status to 'Cancelled'
$update_order_sql = "UPDATE orders SET status = 'Cancelled' WHERE id = ?";
$stmt = $conn->prepare($update_order_sql);
$stmt->bind_param("i", $order_id);
if ($stmt->execute()) {
header("Location: customer_orders.php?message=Order Cancelled Successfully!");
exit();
} else {
echo "Error cancelling order: " . $conn->error;
}
?>