prepare('SELECT * FROM users WHERE id = ?'); $stmt->execute([$user_id]); $user = $stmt->fetch(); // Fetch user's communities $stmt = $pdo->prepare('SELECT c.* FROM communities c JOIN community_members cm ON c.id = cm.community_id WHERE cm.user_id = ?'); $stmt->execute([$user_id]); $communities = $stmt->fetchAll(); // Fetch user's discussions $stmt = $pdo->prepare('SELECT * FROM discussions WHERE user_id = ? ORDER BY created_at DESC'); $stmt->execute([$user_id]); $discussions = $stmt->fetchAll(); $name = $user['name']; $city = $user['city']; $errors = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name'] ?? ''); $city = trim($_POST['city'] ?? ''); if (empty($name)) { $errors[] = 'Name is required'; } if (empty($city)) { $errors[] = 'City is required'; } if (empty($errors)) { try { $stmt = $pdo->prepare('UPDATE users SET name = ?, city = ? WHERE id = ?'); $stmt->execute([$name, $city, $user_id]); header('Location: profile.php'); exit; } catch (PDOException $e) { $errors[] = 'Database error: ' . $e->getMessage(); } } } ?>