diff --git a/bunks.php b/bunks.php index 3f01e38..e9405d8 100644 --- a/bunks.php +++ b/bunks.php @@ -4,28 +4,67 @@ require_once 'db/config.php'; $notification = null; -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_bunk'])) { - $name = trim($_POST['name']); - $owner = trim($_POST['owner']); - $contact = trim($_POST['contact']); - $location = trim($_POST['location']); +// Handle Add, Edit, Delete actions +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $db = db(); + // Add Bunk + if (isset($_POST['add_bunk'])) { + $name = trim($_POST['name']); + $owner = trim($_POST['owner']); + $contact = trim($_POST['contact']); + $location = trim($_POST['location']); - if (!empty($name)) { - try { - $db = db(); - $stmt = $db->prepare("INSERT INTO bunks (name, owner, contact, location) VALUES (?, ?, ?, ?)"); - $stmt->execute([$name, $owner, $contact, $location]); - $_SESSION['notification'] = ['text' => 'Bunk added successfully!', 'type' => 'success']; - } catch (PDOException $e) { - $_SESSION['notification'] = ['text' => 'Error adding bunk: ' . $e->getMessage(), 'type' => 'danger']; + if (!empty($name)) { + try { + $stmt = $db->prepare("INSERT INTO bunks (name, owner, contact, location) VALUES (?, ?, ?, ?)"); + $stmt->execute([$name, $owner, $contact, $location]); + $_SESSION['notification'] = ['text' => 'Bunk added successfully!', 'type' => 'success']; + } catch (PDOException $e) { + $_SESSION['notification'] = ['text' => 'Error adding bunk: ' . $e->getMessage(), 'type' => 'danger']; + } + } else { + $_SESSION['notification'] = ['text' => 'Bunk name is required.', 'type' => 'warning']; + } + } + // Edit Bunk + elseif (isset($_POST['edit_bunk'])) { + $id = $_POST['bunk_id']; + $name = trim($_POST['name']); + $owner = trim($_POST['owner']); + $contact = trim($_POST['contact']); + $location = trim($_POST['location']); + + if (!empty($name) && !empty($id)) { + try { + $stmt = $db->prepare("UPDATE bunks SET name = ?, owner = ?, contact = ?, location = ? WHERE id = ?"); + $stmt->execute([$name, $owner, $contact, $location, $id]); + $_SESSION['notification'] = ['text' => 'Bunk updated successfully!', 'type' => 'success']; + } catch (PDOException $e) { + $_SESSION['notification'] = ['text' => 'Error updating bunk: ' . $e->getMessage(), 'type' => 'danger']; + } + } else { + $_SESSION['notification'] = ['text' => 'Bunk name and ID are required.', 'type' => 'warning']; } - } else { - $_SESSION['notification'] = ['text' => 'Bunk name is required.', 'type' => 'warning']; } header("Location: bunks.php"); exit; } +// Delete Bunk +if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) { + try { + $db = db(); + $stmt = $db->prepare("DELETE FROM bunks WHERE id = ?"); + $stmt->execute([$_GET['id']]); + $_SESSION['notification'] = ['text' => 'Bunk deleted successfully!', 'type' => 'success']; + } catch (PDOException $e) { + $_SESSION['notification'] = ['text' => 'Error deleting bunk: ' . $e->getMessage(), 'type' => 'danger']; + } + header("Location: bunks.php"); + exit; +} + + if (isset($_SESSION['notification'])) { $notification = $_SESSION['notification']; unset($_SESSION['notification']); @@ -70,24 +109,7 @@ try { - +
@@ -125,8 +147,20 @@ try { - - + + @@ -174,6 +208,63 @@ try {
+ + + + + + +