Cannot get user info to show up

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Yes, the login part works. I've used my correct username and it's logged me in, but I can't get the welcome message up with the user's username displayed.

What would you suggest?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Cannot get user info to show up

Post by John Cartwright »

khushbush wrote:Yes, the login part works. I've used my correct username and it's logged me in, but I can't get the welcome message up with the user's username displayed.

What would you suggest?
You understand the implications of this? If I know the admins username I can log in with this permissions no problem..

As for not being able to login, try adding some debugging..

Code: Select all

$query = "SELECT username FROM user WHERE userid = $user";
$result = mysql_query($query) or die(mysql_error());
 
echo 'Users found: '. mysql_num_rows($result));
echo '<pre>'. print_r(mysql_fetch_assoc($result), true) .'</pre>';
 
echo '.Welcome <b>'. $row['username'] .'</b>, you are logged in. <br><br>'
       .'[<a href="userinfo.php?user='. $row['username'].'">My Details</a>] &nbsp;&nbsp;'
       .'[<a href="useredit.php">Edit Account</a>] &nbsp;&nbsp;';
 
exit();
 
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Oh yes, I will be including some password verification too. Thanks for the reminder though.

I'm just trying to get this to work first. And...it doesn't.

It showed the logout link though. And the 'Logged In' title. That's it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Cannot get user info to show up

Post by John Cartwright »

khushbush wrote:Oh yes, I will be including some password verification too. Thanks for the reminder though.

I'm just trying to get this to work first. And...it doesn't.

It showed the logout link though. And the 'Logged In' title. That's it.
Please post the exact output of the script I posted.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Here is the output:

Logged In

[Logout]
Last edited by khushbush on Tue Apr 08, 2008 11:30 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Cannot get user info to show up

Post by John Cartwright »

What is the output of this code

Code: Select all

$query = "SELECT username FROM user WHERE userid = $user";
$result = mysql_query($query) or die(mysql_error());
 
echo 'Users found: '. mysql_num_rows($result));
echo '<pre>'. print_r(mysql_fetch_assoc($result), true) .'</pre>';
 
echo '.Welcome <b>'. $row['username'] .'</b>, you are logged in. <br><br>'
       .'[<a href="userinfo.php?user='. $row['username'].'">My Details</a>] &nbsp;&nbsp;'
       .'[<a href="useredit.php">Edit Account</a>] &nbsp;&nbsp;';
 
exit();
 
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Oh...

Here's the output I got:

Logged In

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

I hope that that is what you meant...
User avatar
bovermyer
Forum Commoner
Posts: 25
Joined: Tue Apr 08, 2008 9:14 am
Location: South Dakota

Re: Cannot get user info to show up

Post by bovermyer »

Your SQL error is this:

$query = "SELECT username FROM user WHERE userid = $user";

It should be '$user', not $user.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Thanks, bovermyer...nearly there...

Here's what I got after making the changes:

Logged In
Users found: 0

Welcome , you are logged in.

[My Details] [Edit Account]

Username still not showing up and it's not counting the number of users in the database.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Ok, the current code so far:

Code: Select all

 
   echo "<h1>Logged In</h1>";
 
   $user = mysql_real_escape_string($_GET['userid']);
   $query = "SELECT username FROM user WHERE userid = '$user'";
$result = mysql_query($query) or die(mysql_error());
 
    
echo 'Users found: '. mysql_num_rows($result);
echo '<pre>'. print_r(mysql_fetch_assoc($result), true) .'</pre>';
 
echo 'Welcome <b>'. $row['username'] .'</b>, you are logged in. <br><br>'
       .'[<a href="userinfo.php?user='. $row['username'].'">My Details</a>] &nbsp;&nbsp;'
       .'[<a href="useredit.php">Edit Account</a>] &nbsp;&nbsp;';
 
exit();
 
I've realised that the problem lies within not specifying $row as a variable. $row should be:

Code: Select all

$row=mysql_fetch_row($result);
I tried that...and it didn't work. Is there anything else that might work? Could it be that the GET method doesn't work either?
User avatar
bovermyer
Forum Commoner
Posts: 25
Joined: Tue Apr 08, 2008 9:14 am
Location: South Dakota

Re: Cannot get user info to show up

Post by bovermyer »

Could you post all of your code so far as a .zip archive? I'd like to see everything related to the authentication.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

I GOT IT WORKING!!! :D :D :D

Here's the code:

Code: Select all

 
$user = mysql_real_escape_string($_SESSION['userid']);
   $query = "SELECT username FROM user WHERE userid = '$user'";
$result = mysql_query($query) or die(mysql_error());
 
   echo "Welcome <b>". $_SESSION['username'] ."</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$session->username\">My Details</a>] &nbsp;&nbsp;"
       ."[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;";
   if($session->isAdmin()){
      echo "[<a href=\"admin.php\">Admin Center</a>] &nbsp;&nbsp;";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
 
else {
  header("Location: main.php");
}
?>
 
I just had to specify $_SESSION within a variable...and then within the html...and it showed the username!! YAY!! I'm really very happy! :D

Thanks ever so much for all your help, everyone! :)
User avatar
bovermyer
Forum Commoner
Posts: 25
Joined: Tue Apr 08, 2008 9:14 am
Location: South Dakota

Re: Cannot get user info to show up

Post by bovermyer »

Glad you got it figured out. 8)
Post Reply