Page 1 of 1

if statement / mysql query

Posted: Thu Feb 11, 2010 10:50 am
by aravona
The following code is testing my patience because I know I should know what is wrong and I can't get it working. This is just a simple exercise for me to keep my php skills up to scratch whilst I hack and slash other peoples sites and fix them / do text changes etc.

Whats happening is I'm sent to the rediect every time. I think that my $userlevel isn't being set to admin or member... If I have ($userlevel = "admin") with one equals not too that information is echoed, even if I have logged into the user with 'member' status. If i set if ($userlevel == "admin") with two equals and if ($userlevel = "member") has one equals I go to member. Hence I think something is wrong with my query.

Code: Select all

session_start();
    include("validate.php");
    include("connection.php");
 
    if ($_SESSION['id'] != '' or validatelogin($_POST['txtid'],$_POST['txtpwd']) == true){
    
    $_SESSION['id'] = $_POST['txtid'];
    $sql = "Select userlevel from userdetails where username='" . $_SESSION['id'] . "'";
    $userlevel = mysql_query($sql, $conn);
    
    if ($userlevel == "admin")
    {
    echo "You are now logged in as " . $_SESSION['id'] . " your level is Administrator";
    echo "<br><a href='newpage.php'>User Control</a>";
    echo "<br><a href='adminindex.php'>Enter Administration Area</a>";
    }
    elseif ($userlevel == "member")
    {
    echo "You are now logged in as " . $_SESSION['id'] . " your level is Member";
    echo "<br><a href='newpage.php'>User Control</a>";
    }
    else 
    {
    echo "You have posted incorrect log in data you will automatically be redirected";
    echo "<head><meta HTTP-EQUIV='REFRESH' content='10; url=home.html'></head>";
    }
    }
I know its likely going to be obivous but I haven't done any 'start from scratch' php coding in a long while ^_^'

Re: if statement / mysql query

Posted: Thu Feb 11, 2010 11:07 am
by AbraCadaver
mysql_query doesn't return a field from the database does it? Have you used mysql_fetch_array() or similar?

Code: Select all

$userlevel = mysql_query($sql, $conn);
Maybe you should do some basic troubleshooting, like echo $userlevel; etc...

Re: if statement / mysql query

Posted: Thu Feb 11, 2010 2:36 pm
by limitdesigns
He's right...you can't get any data besides a MySQL object from mysql_query. You have to get the data using mysql_fetch_array().

Re: if statement / mysql query

Posted: Fri Feb 12, 2010 3:06 am
by aravona
Yeah I did echo $userlevel and got nothing - so I did try

Code: Select all

 
$sql = "Select userlevel from userdetails where username='" . $_SESSION['id'] . "'";
$result = mysql_query($sql, $conn);
$userlevel = mysql_fetch_array($result);
echo $userlevel;
 
 
the echo'd result just says 'Array'

I've been looking through the php manual aswell but I don't think it explains stuff too well (at least not as I'm use to)

Re: if statement / mysql query

Posted: Fri Feb 12, 2010 4:23 am
by aravona

Code: Select all

    
$_SESSION['id'] = $_POST['txtid'];
$sql = "Select userlevel from userdetails where username='" . $_SESSION['id'] . "'";
$result = mysql_query($sql, $conn);
$userlevel = mysql_fetch_array($result);
echo $userlevel[0];
 
Ok that works and I either get 'admin' or 'member' but either way I'm still sent to the 'admin' part of the if statement... with this:

Code: Select all

 
    if ($userlevel = "admin")
    {
    echo "You are now logged in as " . $_SESSION['id'] . " your level is Administrator";
    echo "<br><a href='newpage.php'>User Control</a>";
    echo "<br><a href='adminindex.php'>Enter Administration Area</a>";
    }
    elseif ($userlevel = "member")
    {
    echo "You are now logged in as " . $_SESSION['id'] . " your level is Member";
    echo "<br><a href='newpage.php'>User Control</a>";
    }
    else 
    {
    echo "You have posted incorrect log in data you will automatically be redirected";
    echo "<head><meta HTTP-EQUIV='REFRESH' content='10; url=home.html'></head>";
    }
    }
Or redirected with this:

Code: Select all

 
    if ($userlevel == "admin")
    {
    echo "You are now logged in as " . $_SESSION['id'] . " your level is Administrator";
    echo "<br><a href='newpage.php'>User Control</a>";
    echo "<br><a href='adminindex.php'>Enter Administration Area</a>";
    }
    elseif ($userlevel == "member")
    {
    echo "You are now logged in as " . $_SESSION['id'] . " your level is Member";
    echo "<br><a href='newpage.php'>User Control</a>";
    }
    else 
    {
    echo "You have posted incorrect log in data you will automatically be redirected";
    echo "<head><meta HTTP-EQUIV='REFRESH' content='10; url=home.html'></head>";
    }
    }