prepare('SELECT role FROM users WHERE id = ?'); $stmt->execute([$user_id]); $user_role = $stmt->fetchColumn(); if ($user_role !== 'leader') { // Redirect non-leaders header('Location: communities.php'); exit; } // Handle delete if (isset($_GET['delete'])) { $community_id_to_delete = $_GET['delete']; // further check if the current user is the owner of the community $stmt = $pdo->prepare('DELETE FROM communities WHERE id = ? AND leader_id = ?'); $stmt->execute([$community_id_to_delete, $user_id]); header('Location: manage_communities.php'); exit; } // Handle form submission for creating/editing $name = $description = ''; $errors = []; $edit_id = null; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name'] ?? ''); $description = trim($_POST['description'] ?? ''); $edit_id = $_POST['edit_id'] ?? null; if (empty($name)) { $errors[] = 'Community name is required'; } if (empty($errors)) { try { if ($edit_id) { // Update existing community $stmt = $pdo->prepare('UPDATE communities SET name = ?, description = ? WHERE id = ? AND leader_id = ?'); $stmt->execute([$name, $description, $edit_id, $user_id]); } else { // Create new community $stmt = $pdo->prepare('INSERT INTO communities (name, description, leader_id) VALUES (?, ?, ?)'); $stmt->execute([$name, $description, $user_id]); } header('Location: manage_communities.php'); exit; } catch (PDOException $e) { $errors[] = 'Database error: ' . $e->getMessage(); } } } // Fetch community to edit if an id is passed if (isset($_GET['edit'])) { $edit_id = $_GET['edit']; $stmt = $pdo->prepare('SELECT * FROM communities WHERE id = ? AND leader_id = ?'); $stmt->execute([$edit_id, $user_id]); $community_to_edit = $stmt->fetch(); if ($community_to_edit) { $name = $community_to_edit['name']; $description = $community_to_edit['description']; } } // Fetch all communities led by the user $stmt = $pdo->prepare('SELECT * FROM communities WHERE leader_id = ? ORDER BY created_at DESC'); $stmt->execute([$user_id]); $communities = $stmt->fetchAll(); ?> Manage Communities - Community Hub

Manage Communities

Cancel Edit

Your Communities

You have not created any communities yet.

Edit Delete