35429-vm/add_expense.php
2025-11-02 20:12:50 +00:00

31 lines
1.1 KiB
PHP

<?php
require_once 'db/config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$expense_date = $_POST['expense_date'] ?? null;
$description = $_POST['description'] ?? null;
$amount = $_POST['amount'] ?? null;
$category = $_POST['category'] ?? null;
if ($expense_date && $description && $amount) {
try {
$pdo = db();
$sql = "INSERT INTO expenses (expense_date, description, amount, category) VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$expense_date, $description, $amount, $category]);
// Redirect back to the expenses page with a success message
header("Location: expenses.php?success=1");
exit();
} catch (PDOException $e) {
// Handle error, maybe redirect with an error message
header("Location: expenses.php?error=" . urlencode($e->getMessage()));
exit();
}
} else {
// Handle invalid input
header("Location: expenses.php?error=invalid_input");
exit();
}
}
?>