query("SELECT * FROM prices ORDER BY updated_at DESC LIMIT 1"); $latest_prices = $stmt->fetch(); if (!$latest_prices) { $_SESSION['order_error'] = "Could not retrieve latest prices. Please try again later."; header('Location: dashboard.php'); exit; } $price = ($fuel_type === 'petrol') ? $latest_prices['petrol_price'] : $latest_prices['diesel_price']; $total_price = $quantity * $price; // Insert the order $sql = "INSERT INTO orders (user_id, fuel_type, quantity, total_price) VALUES (:user_id, :fuel_type, :quantity, :total_price)"; $stmt = $pdoconn->prepare($sql); $stmt->execute([ ':user_id' => $user_id, ':fuel_type' => $fuel_type, ':quantity' => $quantity, ':total_price' => $total_price ]); $_SESSION['order_success'] = "Your order has been placed successfully!"; header('Location: dashboard.php'); exit; } catch (PDOException $e) { error_log("Order submission failed: " . $e->getMessage()); $_SESSION['order_error'] = "There was an error placing your order. Please try again."; header('Location: dashboard.php'); exit; } } else { // Redirect if accessed directly header('Location: dashboard.php'); exit; } ?>