prepare($sql); foreach ($budgets as $category => $amount) { if (is_numeric($amount) && $amount >= 0) { $stmt->execute([ 'user_id' => $user_id, 'category' => $category, 'amount' => $amount, 'budget_month' => $posted_month ]); } } $success_message = 'Orçamentos salvos com sucesso!'; // Redirecionar para o mesmo mês para mostrar a atualização header('Location: budgets.php?month=' . date('Y-m', strtotime($posted_month))); exit; } catch (PDOException $e) { $error_message = 'Erro ao salvar orçamentos: ' . $e->getMessage(); } } // Buscar orçamentos existentes para o mês selecionado $existing_budgets = []; try { $stmt = $pdo->prepare("SELECT category, amount FROM budgets WHERE user_id = :user_id AND budget_month = :budget_month"); $stmt->execute(['user_id' => $user_id, 'budget_month' => $budget_month_date]); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($results as $row) { $existing_budgets[$row['category']] = $row['amount']; } } catch (PDOException $e) { $error_message = "Erro ao buscar orçamentos existentes."; } // Categorias fixas $categories = ['Alimentação', 'Transporte', 'Moradia', 'Lazer', 'Saúde', 'Outros']; include __DIR__ . '/includes/header.php'; ?>