if statement / mysql query

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

Post Reply
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

if statement / mysql query

Post 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 ^_^'
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: if statement / mysql query

Post 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...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
limitdesigns
Forum Commoner
Posts: 25
Joined: Sat Feb 06, 2010 9:05 pm

Re: if statement / mysql query

Post 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().
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement / mysql query

Post 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)
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: if statement / mysql query

Post 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>";
    }
    }
Post Reply