prepare("DELETE FROM elective_groups WHERE id = ? AND school_id = ?"); $stmt->execute([$delete_id, $school_id]); $message = "Elective group deleted successfully."; } catch (PDOException $e) { if ($e->getCode() == '23000') { // Integrity constraint violation $error = "Cannot delete this elective group because it has subjects associated with it. Please remove those associations before deleting."; } else { $error = "Error deleting group: " . $e->getMessage(); } } } // Handle POST request to add or update a group if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['group_name'])) { $groupName = trim($_POST['group_name']); $group_id = $_POST['group_id'] ?? null; if (empty($groupName)) { $error = 'Group name cannot be empty.'; } else { // Check for duplicates before inserting $stmt = $pdo->prepare("SELECT id FROM elective_groups WHERE name = ? AND school_id = ? AND id != ?"); $stmt->execute([$groupName, $school_id, $group_id ?? 0]); if ($stmt->fetch()) { $error = "Error: Elective group '" . htmlspecialchars($groupName) . "' already exists."; } else { try { if ($group_id) { // Update existing group $stmt = $pdo->prepare("UPDATE elective_groups SET name = ? WHERE id = ? AND school_id = ?"); $stmt->execute([$groupName, $group_id, $school_id]); $message = "Elective group updated successfully!"; } else { // Insert new group $stmt = $pdo->prepare("INSERT INTO elective_groups (name, school_id) VALUES (?, ?)"); $stmt->execute([$groupName, $school_id]); $message = "Elective group created successfully!"; } } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } } } } // Handle Edit request if (isset($_GET['edit_id'])) { try { $edit_id = $_GET['edit_id']; $stmt = $pdo->prepare("SELECT * FROM elective_groups WHERE id = ? AND school_id = ?"); $stmt->execute([$edit_id, $school_id]); $editing_group = $stmt->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = "Error fetching group: " . $e->getMessage(); } } // Fetch all groups to display $groups = []; try { $groups_stmt = $pdo->prepare("SELECT * FROM elective_groups WHERE school_id = ? ORDER BY name ASC"); $groups_stmt->execute([$school_id]); $groups = $groups_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } ?> Admin: Manage Elective Groups - Haki Schedule

Manage Elective Groups

Cancel Edit
Existing Elective Groups

No elective groups have been created yet.

Name Actions
Edit