I am trying to get the usernames of users to appear in the url so that I can use $_GET['username'] to grab the username to create a link to the user's profile which can be viewed by any logged in user. So far I can only the logged in user's username to display in url.

So I did a loop/queried the database and selected usernames of all users and created a link of the usernames to appear in the url. So any logged in user can go to the users.php and click on any username to see the detailed information of that user. Only logged in users will be able to access the profile.php and users.php.

So far this is what I have

session_start();

  include 'includes/dbh.php';
  $sql = "SELECT * FROM users";
  $query = $conn->query($sql);
  $num = $query->num_rows;
  if($num > 0){
      while($row = $query->fetch_assoc()){
        $users[]= $row;
          foreach($users as $user){
                $usersUsername = $user['usersUsername'];
                echo '<a href="profile.php?user="'.$usersUsername.'">'.$usersUsername.'</a><br>';
          }     
      }
  }

But the url appears in the browser like this: http://localhost/object/profile.php?user=
I need it be something like http://localhost/object/profile.php?user=nanakumi75
The username is missing. I need some help here

echo '<a href="profile.php?user="'.$usersUsername.'">'.$usersUsername.'</a><br>';

should be:

echo '<a href="profile.php?user='.$usersUsername.'">'.$usersUsername.'</a><br>'; // removed one double-quote

Thanks a lot brother. You're right. It worked.

Be sure to properly sanitize output!!!!!

echo '<a href="profile.php?user='. urlencode($usersUsername) .'">'. htmlspecialchars($usersUsername) .'</a><br>'; // removed one double-quote
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.