<?php
session_start();
include "dbconnect.php"; // Database connection

// Check if form data is submitted and if expense_id is provided
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['expense_id'])) {
    $expense_id = $_POST['expense_id'];
    $category = $_POST['expense_category'];
    $amount = $_POST['amount'];
    $date = $_POST['date'];
    $description = $_POST['description'];

    // Prepare the SQL query to update the expense record
    $sql = "UPDATE expenses SET expense_category = ?, amount = ?, date = ?, description = ? WHERE id = ?";

    if ($stmt = $con->prepare($sql)) {
        $stmt->bind_param("ssssi", $category, $amount, $date, $description, $expense_id);

        // Execute the query and check if it was successful
        if ($stmt->execute()) {
            $_SESSION['message'] = "Expense updated successfully!";
        } else {
            $_SESSION['error'] = "Error updating expense.";
        }

        $stmt->close();
    } else {
        $_SESSION['error'] = "Database error.";
    }
} else {
    $_SESSION['error'] = "Invalid request.";
}

// Redirect to the expenses page after the update operation
header("Location: expenses_page.php");
exit();
?>
