RSVPs
Attending
Not Attending
prepare('SELECT role FROM users WHERE id = ?'); $stmt->execute([$_SESSION['user_id']]); $user_role = $stmt->fetchColumn(); } if (!isset($_GET['id'])) { header('Location: communities.php'); exit; } $event_id = $_GET['id']; $user_id = $_SESSION['user_id']; $pdo = db(); // Fetch event details $stmt = $pdo->prepare('SELECT e.*, u.name as user_name, c.name as community_name FROM events e JOIN users u ON e.user_id = u.id JOIN communities c ON e.community_id = c.id WHERE e.id = ?'); $stmt->execute([$event_id]); $event = $stmt->fetch(); if (!$event) { header('Location: communities.php'); exit; } // Handle RSVP if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rsvp'])) { $rsvp = $_POST['rsvp']; if (in_array($rsvp, ['attending', 'not_attending'])) { try { // Check if user has already RSVPed $stmt = $pdo->prepare('SELECT id FROM event_rsvps WHERE user_id = ? AND event_id = ?'); $stmt->execute([$user_id, $event_id]); if ($stmt->fetch()) { // Update existing RSVP $stmt = $pdo->prepare('UPDATE event_rsvps SET rsvp = ? WHERE user_id = ? AND event_id = ?'); $stmt->execute([$rsvp, $user_id, $event_id]); } else { // Insert new RSVP $stmt = $pdo->prepare('INSERT INTO event_rsvps (event_id, user_id, rsvp) VALUES (?, ?, ?)'); $stmt->execute([$event_id, $user_id, $rsvp]); } header('Location: event.php?id=' . $event_id); exit; } catch (PDOException $e) { $errors[] = 'Database error: ' . $e->getMessage(); } } } // Fetch RSVPs $stmt = $pdo->prepare('SELECT rsvp, COUNT(*) as count FROM event_rsvps WHERE event_id = ? GROUP BY rsvp'); $stmt->execute([$event_id]); $rsvps = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $attending = $rsvps['attending'] ?? 0; $not_attending = $rsvps['not_attending'] ?? 0; // Check if the current user has RSVPed $stmt = $pdo->prepare('SELECT rsvp FROM event_rsvps WHERE user_id = ? AND event_id = ?'); $stmt->execute([$user_id, $event_id]); $user_rsvp = $stmt->fetchColumn(); ?>
Attending
Not Attending