BrendanMullanKT Posted September 29, 2010 Share Posted September 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214668-help-with-user-listprofile/ Share on other sites More sharing options...
pengu Posted September 29, 2010 Share Posted September 29, 2010 Look into the $_GET function. Also mysql_real_escape_string($variable) Quote Link to comment https://forums.phpfreaks.com/topic/214668-help-with-user-listprofile/#findComment-1116941 Share on other sites More sharing options...
BrendanMullanKT Posted September 29, 2010 Author Share Posted September 29, 2010 I am using $_POST for my login form, so $_GET wont work. Quote Link to comment https://forums.phpfreaks.com/topic/214668-help-with-user-listprofile/#findComment-1116942 Share on other sites More sharing options...
BrendanMullanKT Posted September 29, 2010 Author Share Posted September 29, 2010 Bump Quote Link to comment https://forums.phpfreaks.com/topic/214668-help-with-user-listprofile/#findComment-1116996 Share on other sites More sharing options...
JasonLewis Posted September 29, 2010 Share Posted September 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214668-help-with-user-listprofile/#findComment-1117005 Share on other sites More sharing options...
yaMz Posted September 29, 2010 Share Posted September 29, 2010 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/214668-help-with-user-listprofile/#findComment-1117019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.