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; } $reply_id = $_GET['id']; $user_id = $_SESSION['user_id']; $pdo = db(); // Fetch reply $stmt = $pdo->prepare('SELECT * FROM discussion_replies WHERE id = ?'); $stmt->execute([$reply_id]); $reply = $stmt->fetch(); if (!$reply) { header('Location: communities.php'); exit; } // Fetch user role $stmt = $pdo->prepare('SELECT role FROM users WHERE id = ?'); $stmt->execute([$user_id]); $user_role = $stmt->fetchColumn(); // Check if user is authorized to edit if ($reply['user_id'] != $user_id && $user_role != 'leader') { header('Location: discussion.php?id=' . $reply['discussion_id']); exit; } $content = $reply['content']; $errors = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $content = trim($_POST['content'] ?? ''); if (empty($content)) { $errors[] = 'Content is required'; } if (empty($errors)) { try { $stmt = $pdo->prepare('UPDATE discussion_replies SET content = ? WHERE id = ?'); $stmt->execute([$content, $reply_id]); header('Location: discussion.php?id=' . $reply['discussion_id']); exit; } catch (PDOException $e) { $errors[] = 'Database error: ' . $e->getMessage(); } } } ?> Edit Reply - Community Hub

Edit Reply

Cancel