| Linux premium274.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64 Path : /home/whagcoha/ledger.mwagalwaservices.com/ |
| Current File : /home/whagcoha/ledger.mwagalwaservices.com/editexpenses.php |
<?php
if ((!isset($_SESSION['gen256'])) ) {
echo "<script>
window.open('login.php','_self');
</script>";
}
?>
<?php
$success = $error = '';
$user_id = $_SESSION['gen256']['id'];
// Check if id is passed
if (!isset($_GET['id'])) {
die("Invalid request. Expense ID is missing.");
}
$expense_id = (int)$_GET['id'];
// Fetch existing expense
$stmt = $conn->prepare("SELECT * FROM expenses WHERE id=? AND user_id=?");
$stmt->bind_param("ii", $expense_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
$expense = $result->fetch_assoc();
$stmt->close();
if (!$expense) {
die("Expense not found or you don’t have permission.");
}
// Handle update
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$category = $_POST['category'];
$amount = $_POST['amount'];
$date = $_POST['date'];
$notes = $_POST['notes'];
$categorytype = $_POST['categorytype'];
$saving = isset($_POST['saving']) ? 1 : 0;
$stmt = $conn->prepare("UPDATE expenses
SET category=?, categorytype=?, amount=?, date=?, notes=?, saving=?
WHERE id=? AND user_id=?");
if ($stmt) {
$stmt->bind_param("ssdssiii", $category, $categorytype, $amount, $date, $notes, $saving, $expense_id, $user_id);
if ($stmt->execute()) {
$success = "Expense updated successfully!";
// refresh data
$expense = [
"category" => $category,
"categorytype" => $categorytype,
"amount" => $amount,
"date" => $date,
"notes" => $notes,
"saving" => $saving
];
echo "<script>
window.location.href = 'index.php?p=editexpenses&&id=$expense_id';
</script>";
} else {
$error = "Update failed: " . $stmt->error;
}
$stmt->close();
} else {
$error = "Prepare failed: " . $conn->error;
}
}
?>
<div class="container mt-5">
<h2 class="mb-4">Edit Expense</h2>
<?php if (!empty($success)): ?>
<div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
<?php elseif (!empty($error)): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="POST" action="">
<div class="mb-3">
<label class="form-label">Category Description</label>
<input type="text" name="category" class="form-control" value="<?= htmlspecialchars($expense['category']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Category Type</label>
<select class="form-control" name="categorytype" required>
<option value="Generic" <?= $expense['categorytype']=='Generic'?'selected':'' ?>>Nothing To Select</option>
<?php
$result = $conn->query("SELECT name FROM categories WHERE user_id = '$user_id'");
while ($row = $result->fetch_assoc()) {
$selected = ($expense['categorytype'] == $row['name']) ? 'selected' : '';
echo '<option value="' . htmlspecialchars($row['name']) . '" ' . $selected . '>' . htmlspecialchars($row['name']) . '</option>';
}
?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Amount</label>
<input type="number" step="0.01" name="amount" class="form-control" value="<?= htmlspecialchars($expense['amount']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Date</label>
<input type="date" name="date" class="form-control" value="<?= htmlspecialchars($expense['date']) ?>" required>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="loanCheck" name="saving" value="1" <?= $expense['saving'] ? 'checked' : '' ?>>
<label class="form-check-label" for="loanCheck">Mark as Saving</label>
</div>
<div class="mb-3">
<label class="form-label">Notes</label>
<textarea name="notes" class="form-control" rows="3"><?= htmlspecialchars($expense['notes']) ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Update Expense</button>
<a href="index.php?p=viewexpenses" class="btn btn-secondary">Back</a>
</form>
</div>