<?php
// Include your database connection code here (e.g., dbconn.php)
require_once 'includes/dbconn.php';

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form data
    $expense_name = $_POST["expense_name"];
    $category = $_POST["category"];
    $expense_date = $_POST["expense_date"];
    $description = $_POST["description"];
    $amount = $_POST["amount"];
    $user_id = $_POST["user_id"];
    $company_id = $_POST["company_id"];

    // Get the current capture date
    $capture_date = date('Y-m-d H:i:s');

    // Perform database insertion
    $sql = "INSERT INTO expenses (expense_name, category, expense_date, description, amount, user_id, capture_date, company_id) 
            VALUES ('$expense_name', '$category', '$expense_date', '$description', '$amount', '$user_id', '$capture_date','$company_id')";

    if ($conn->query($sql) === TRUE) {
        // Redirect to the same page after successful submission
        header("Location: ".$_SERVER['HTTP_REFERER']);
        exit();
    } else {
        // Handle the error, you might want to display an error message or log the error
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
} else {
    echo "Form not submitted.";
}

// Close the database connection
$conn->close();
?>
