Jump to content

if elseif statement


woodplease

Recommended Posts

i'm having trouble with an elseif statement. its not displaying the result it should

?php
if(isset($_COOKIE['ID_forum'])){

$username = $_COOKIE['ID_forum'];

$query = "SELECT * FROM users WHERE username = '".$username."'";
$level = mysql_query($query) or die ("Select Error :" . mysql_error());
$userlevel = $level['user_level'];
echo $userlevel;

if ($userlevel == "1") {
echo 'you are a member';	
}
elseif ($userlevel == "2") {
echo 'you are a moderator';	
}
elseif ($userlevel == "3") {
echo 'you are an administrator ';	
}
else
echo'you are not logged in';
}
?>

 

it is echoing out 'you are not logged in', when it should be saying 'you are an administrator', as the value in the table is 3. i do know that the cookie 'ID_forum' is there, but i cant find why its not working properly. Any ideas would be great.

 

Thanks

Link to comment
Share on other sites

i forgot that was there. i was using it to check if the query was working or not, it should have returned the value from the table where the username is that of the one stored in the cookie, either a 1, 2 or 3.

i've just realised that if its not working, then theres a problem with the query, but looking at it, i cant see what the problem is.

Link to comment
Share on other sites

Here, try this. However, using a cookie value to determine the user's access level is ill-advised. Cookies can be manipulated by the user; you'd be better off to use session data.

 

Also, you should avoid wildcard SELECT * statements unless all of the fields are actually going to be used. Just explicitly name the fields you need in the query string.

 

$query = "SELECT `user_level` FROM `users` WHERE `username` = '" . mysql_real_escape_string($username) . "'";
$result = mysql_query($query) or die ("Select Error :" . mysql_error());
$array = mysql_fetch_assoc($result);
$userlevel = $array['user_level'];
echo $userlevel;

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.