Jump to content

Help With User List/Profile


BrendanMullanKT

Recommended Posts

Hello all, i require some assistance in a bit of PHP/MySql code.

 

I have a website setup with register/login scripts already wrote, i also have a basic members page for now, that has there user ID assigned to it for example members.php?id=$id, which is there ID from the database.

 

I have a members list which shows all members with links to there profiles, now i when i mouse over the link, it will says members.php?id=1 and so on, which is correct but when clicking on any of the members to go to there profile it is my own details that is shown on there profile instead of theres.

 

members.php

<?php
session_start();
mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("hireacoder") or die(mysql_error());

$user = $SESSION['username'];
$sql = mysql_query("SELECT * FROM users WHERE username='$user'");
$row = mysql_fetch_assoc($sql);
echo $row['username'];
echo'<br>';
echo $row['fname'];
echo'<br>';
echo $row['lname'];
echo'<a href="users.php">Users</a>';
?>

 

users.php

<?php
session_start();
mysql_connect("localhost","root") or die(mysql_error());

mysql_select_db("hireacoder") or die(mysql_error());
echo "<table border='0'>
<tr>
<th>UserName</th>
</tr>";
$sql = mysql_query("SELECT * FROM users ORDER BY ID");
while($row = mysql_fetch_assoc($sql))
  {
  $id = $row['id'];
  $username = $row['username'];
  echo"
  <tr>
  <td>
  <a href='members.php?id=$id'>".$username."</a>
  </td>
  </tr>";
  }
echo "</table>";
?>

 

Now i know what the problem is, the query is getting the details from the DB with the username = the session user which is me and that is why my details show up on all profiles, but i dont know any other way to do it, any help with be very apprciated thanks you.

Link to comment
Share on other sites

You'll need to check to see if they are wanting to view a members profile by checking the $_GET superglobal.

 

So on members.php, you have it set to always show your information. You need to do some checking first.

 

if(isset($_GET['id'])){
   // they want to view a different member, set the id variable here and perhaps run the query
}else{
   // viewing your profile, run the query to grab the default information
}

// now you can show the information and it'll show depending on if they selected a memeber or not.

Link to comment
Share on other sites

This code will return this information only to the user whom is logged in and has their username set in $_SESSION global.

 

From what I understand , you want this information to be viewed by anyone ,displaying the info from the database. Simply :

 


<?php
session_start();
mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("hireacoder") or die(mysql_error());

if ( isset($_GET['id']) ){
$user = mysql_real_escape_string(strip_tags($_GET['id']));
} 
else {
// do something else i.e. 
// $user = NULL;
// header("Location: /users.php");
// exit;
}

$sql = mysql_query("SELECT * FROM users WHERE id='$user'");
$row = mysql_fetch_assoc($sql);
echo $row['username'];
echo'<br>';
echo $row['fname'];
echo'<br>';
echo $row['lname'];
echo'<a href="users.php">Users</a>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.