ELSEIF problem in php to MySQL database

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
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

ELSEIF problem in php to MySQL database

Post by Cooperman »

Hi there, I am a major newbie to php and have come a bit stuck on a bit of code. The code below works fine but I want to add that in the DB there is a row called "access". If this is set to "1" then you go to guest-area.php, if it's set to "2" then you go to members-area.php

Can anyone help with this code? :cry:

Code: Select all

 
<?php
 
define('SQL_USER', 'username');
define('SQL_PASS', 'password');
define('SQL_DB',  'mydb');
// Create a link to the database server
$link = mysql_connect('localhost', 'username', 'password');
if(!$link) :
   die('Could not connect: ' . mysql_error());
endif;
// Select a database where our member tables are stored
$db = mysql_select_db(SQL_DB, $link);
if(!$db) :
   die ('Can\'t connect to database : ' . mysql_error());
endif;
 
 
session_start();
 
if(isset($_POST['submit'])) :
   $username = strip_tags($_POST['username']);
   $password = strip_tags($_POST['password']);
    // Make the query a wee-bit safer
   $query = sprintf("SELECT L_ID, access FROM login WHERE user = '%s' AND pass = '%s' LIMIT 1;", mysql_real_escape_string($username), mysql_real_escape_string($password)); 
   $result = mysql_query($query);
      if(1 != mysql_num_rows($result)) :
       // MySQL returned zero rows (or there's something wrong with the query)
       header('Location: Login.html?msg=login_failed');
   
   else :
       // We found the row that we were looking for
       $row = mysql_fetch_assoc($result);
       // Register the user ID for further use
       $_SESSION['member_ID'] = $row['L_ID'];
       header('Location: members-area.php');
   endif;
endif;
?>
 
Last edited by Weirdan on Wed Jun 04, 2008 5:52 am, edited 1 time in total.
Reason: php tags
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: ELSEIF problem in php to MySQL database

Post by dbemowsk »

You don't seem to be using the correct if...then syntax.
You are using:
if (...) :

Code: Select all

;
else :

Code: Select all

;
endif;

In PHP the  syntax is:
if (...) {

Code: Select all

;
} else {

Code: Select all

;
}
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: ELSEIF problem in php to MySQL database

Post by Cooperman »

This is where I get stuck as I have used code from the web and changed it to suit my needs. It seems to work but then when I try to add an elseif it fails.

I'm not sure how I would re-write all the code to match how you have it below?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: ELSEIF problem in php to MySQL database

Post by Eran »

Actually what debowsk wrote is incorrect. Both syntax can be used - http://www.php.net/manual/en/control-st ... syntax.php
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: ELSEIF problem in php to MySQL database

Post by Cooperman »

OK, but I am still very very stuck.

Maybe if I do this in easier terms as I am getting lost.

I have a database which I can connect to ok.

In this DB I have a row called "access".

In this row it either has a "1" or a "2" in it.
1 = guest
2 = member

How do I query the database to say if the user has a 1 then go to the guest page and if a 2 then go to the members page?
I tried simplifying the code but it constantly goes to members even if there is a "1" in the access row...

PLEASE HELP!! :cry:


$sql = ("SELECT * FROM login WHERE L_ID = ". $_SESSION['member_ID'] ." AND access == 1");
$result = mysql_query($sql);


if ($result) :

echo ("guest only");

else :

echo ("members only");
endif;
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: ELSEIF problem in php to MySQL database

Post by nowaydown1 »

You're already doing a select on access in your query, so why not just check it?

Code: Select all

 
<?php
 
define('SQL_USER', 'username');
define('SQL_PASS', 'password');
define('SQL_DB', 'mydb');
// Create a link to the database server
$link = mysql_connect('localhost', 'username', 'password');
if(!$link) :
die('Could not connect: ' . mysql_error());
endif;
// Select a database where our member tables are stored
$db = mysql_select_db(SQL_DB, $link);
if(!$db) :
die ('Can\'t connect to database : ' . mysql_error());
endif;
 
 
session_start();
 
if(isset($_POST['submit'])) {
    $username = strip_tags($_POST['username']);
    $password = strip_tags($_POST['password']);
    // Make the query a wee-bit safer
    $query = sprintf("SELECT L_ID, access FROM login WHERE user = '%s' AND pass = '%s' LIMIT 1;", mysql_real_escape_string($username), mysql_real_escape_string($password));
    $result = mysql_query($query);
    
    if(1 != mysql_num_rows($result)) {
        // MySQL returned zero rows (or there's something wrong with the query)
        header('Location: Login.html?msg=login_failed');
    } else {
        // We found the row that we were looking for
        $row = mysql_fetch_assoc($result);
        // Register the user ID for further use
        $_SESSION['member_ID'] = $row['L_ID'];
        
        if($row['access'] == 2) {
            header('Location: members-area.php');
        } elseif($row['access'] == 1) {
            header('Location: members-guest.php');
        }
    }
}
?>
 
Cooperman
Forum Commoner
Posts: 27
Joined: Mon Jun 02, 2008 4:44 am

Re: ELSEIF problem in php to MySQL database

Post by Cooperman »

You're a star, it works a treat. :D

Many thanks for your support :lol:
Post Reply