prepare($query); $stmt->bind_param("i", $expenseId); $stmt->execute(); $result = $stmt->get_result(); $expense = $result->fetch_assoc(); if (!$expense) { header("Location: expenses.php"); exit(); } // Fetch expense categories for dropdown $categoriesQuery = "SELECT id, name FROM expense_categories ORDER BY name"; $categoriesResult = $conn->query($categoriesQuery); $categories = $categoriesResult->fetch_all(MYSQLI_ASSOC); // Fetch expense heads for the current category $headsQuery = "SELECT id, name FROM expense_heads WHERE category_id = ? ORDER BY name"; $headsStmt = $conn->prepare($headsQuery); $headsStmt->bind_param("i", $expense['category_id']); $headsStmt->execute(); $headsResult = $headsStmt->get_result(); $heads = $headsResult->fetch_all(MYSQLI_ASSOC); // Handle form submission if ($_SERVER["REQUEST_METHOD"] == "POST") { $query = "UPDATE expenses SET date = ?, category_id = ?, head_id = ?, amount = ? WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("siidi", $_POST["date"], $_POST["category_id"], $_POST["head_id"], $_POST["amount"], $expenseId); $stmt->execute(); header("Location: expenses.php"); exit(); } $content = <<Edit Expense
HTML; // Include the main layout include 'main_layout.php'; // Helper function to generate category options function generateCategoryOptions($categories, $selectedCategoryId) { $options = ''; foreach ($categories as $category) { $selected = $category['id'] == $selectedCategoryId ? 'selected' : ''; $options .= ""; } return $options; } // Helper function to generate head options function generateHeadOptions($heads, $selectedHeadId) { $options = ''; foreach ($heads as $head) { $selected = $head['id'] == $selectedHeadId ? 'selected' : ''; $options .= ""; } return $options; }