nanakumi75 16 Newbie Poster

I have a long/register system working . Each user see their own profile details when they login. Now I want to users to see other users profile by clicking on a link to their profile.
How do I do this? I need advice

Dani AI

Generated

For — with login/registration already working the simplest pattern is: 1) add profile links that include an identifier (id or username), and 2) make a dedicated profile page that reads that identifier, validates it, queries only non-sensitive fields, and renders the result safely. The example below follows that pattern and focuses on safety (prepared statements, input validation, output escaping) and privacy (never echo password hashes or private data).

// link generation (user list)
echo '<a href="profile.php?id=' . (int)$row['id'] . '">View profile</a>';
// profile.php (minimal, safe)
session_start();
require 'db.php'; // provides $pdo (PDO)

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) { http_response_code(400); echo 'Invalid id'; exit; }

$stmt = $pdo->prepare('SELECT id, username, display_name, bio, avatar, created_at FROM users WHERE id = :id LIMIT 1');
$stmt->execute([':id' => $id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$user) { http_response_code(404); echo 'Profile not found'; exit; }

function h($s){ return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }

echo '<h1>'. h($user['display_name'] ?: $user['username']) .'</h1>';
echo '<p>'. nl2br(h($user['bio'] ?? '')) .'</p>';
if (!empty($_SESSION['user_id']) && $_SESSION['user_id'] == $user['id']) {
  echo '<p><a href="edit-profile.php">Edit profile</a></p>';
}

Troubleshooting and cautions: validate the incoming id (use FILTER_VALIDATE_INT or ctype_digit), always use prepared statements, escape every value put into HTML, and never SELECT password or other secrets for public pages. Consider privacy flags in the DB if some fields must be hidden. If ID enumeration is a concern, expose a username slug instead of numeric ids. Lastly, verify avatar file paths (avoid arbitrary file includes) and return a 404 for nonexistent profiles.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.