SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    parse error in my login page... using a query inside another query.

    Hi

    Problem description:
    I have a login.php page in which I check for permissions when the user is logging in. Everything worked fine. I have just added a 'lastLogin' field in order to check for new texts posted between the last member's login and his new ligin. Problem is: I use a request inside a request and I get a parse error.

    Question:
    How can I use a query inside another quesry? Where does the parse error comes from? Here is the error message: Parse error: parse error in c:\phpdev\www\login.php on line 63

    Code:
    Here is the whole code. Comments and critics are more than welcome by the way.
    PHP Code:
    <?php 
    session_start
    ();
    require_once(
    'db.php'); 
    error_reporting(E_ALL);

    $username strip_tags(trim($_POST['username'])); 
    $password strip_tags(trim($_POST['password']));
    $lastLogin time();



    $sql mysql_query(
     
    "SELECT userID
     , username
     , password
     , permission
     , lastLogin
     FROM users
     WHERE username = '
    $username'
     AND password = '
    $password'
     LIMIT 0,1"

     or die(
    '<p>Unable to query the firststep database at this time.<br />Error: ' mysql_error() . '</p>'); 

    $row mysql_fetch_array($sql); 

    if(
    mysql_num_rows($sql) == 1// mysql_num_rows() returns the record count from the query result.  you want one (1) matching record to verify the login was successful. 


    $userID $row['userID'];

         if (
    $row['permission'] == 2
         {
         
    $_SESSION['loggedin'] = 2// Setting session var 'loggedin' to true, with admin permission.
         
    $_SESSION['username'] = $row['username'];
         
    $_SESSION['userID'] = $userID;
         
    $_SESSION['lastLogin'] = $row['lastLogin'];
         
    $updateLastLogin mysql_query(
             
    "UPDATE users 
             SET 
             lastLogin = 
    $lastLogin
             WHERE userID = 
    $userID
            LIMIT 0,1"
    ); 
            }
         
    header('Location:index.php'); 
         exit; 
         }
         else 
         {
         
    $_SESSION['loggedin'] = 1// Setting session var 'loggedin' to true
         
    $_SESSION['username'] = $row['username'];
         
    $_SESSION['userID'] = $row['userID'];
         
    $_SESSION['userID'] = $userID;
         
    $_SESSION['lastLogin'] = $row['lastLogin'];
         
    $updateLastLogin mysql_query(
             
    "UPDATE users 
             SET 
             lastLogin = 
    $lastLogin
             WHERE userID = 
    $userID
            LIMIT 0,1"
    ); 
            }
         
    header('Location:index.php'); 
         exit; 
         } 
    //-------------------------------------------------------> line 63

    else 

         
    $_SESSION['loggedin'] = 0// Setting session var 'loggedin' to false 
         
    header('Location:error.php'); // Redirect to error page, as a matching record was not found in the table. 
         
    exit;  // always 'exit' 
    }

    ?>
    thanks a lot for your time and your energy in helping me

  2. #2
    SitePoint Addict streetlife's Avatar
    Join Date
    Mar 2004
    Location
    D-Town
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    duuudie, I checked your code and you had two '}' tags too many, down around line 63, so a parse error was returned. I've manipulated your code as follows, removing a '}' from just after each UPDATE query, as it seemed that they might be there by mistake, but I haven't studied the logic of your code so it may be all wrong and you need to add more IF statements or whatever.
    PHP Code:
    <?php 
    session_start
    (); 
    require_once(
    'db.php'); 
    error_reporting(E_ALL); 
    $username strip_tags(trim($_POST['username'])); 
    $password strip_tags(trim($_POST['password'])); 
    $lastLogin time(); 
     
    $sql mysql_query
    "SELECT userID 
    , username 
    , password 
    , permission 
    , lastLogin 
    FROM users 
    WHERE username = '
    $username
    AND password = '
    $password
    LIMIT 0,1"

    or die(
    '<p>Unable to query the firststep database at this time.<br />Error: ' mysql_error() . '</p>'); 
    $row mysql_fetch_array($sql); 
    if(
    mysql_num_rows($sql) == 1// mysql_num_rows() returns the record count from the query result.  you want one (1) matching record to verify the login was successful. 

    $userID $row['userID']; 
         if (
    $row['permission'] == 2
         { 
         
    $_SESSION['loggedin'] = 2// Setting session var 'loggedin' to true, with admin permission. 
         
    $_SESSION['username'] = $row['username']; 
         
    $_SESSION['userID'] = $userID
         
    $_SESSION['lastLogin'] = $row['lastLogin']; 
         
    $updateLastLogin mysql_query
            
    "UPDATE users 
            SET 
            lastLogin = 
    $lastLogin 
            WHERE userID = 
    $userID 
            LIMIT 0,1"
    ); 
         
    header('Location:index.php'); 
         exit; 
         } 
         else 
         { 
         
    $_SESSION['loggedin'] = 1// Setting session var 'loggedin' to true 
         
    $_SESSION['username'] = $row['username']; 
         
    $_SESSION['userID'] = $row['userID']; 
         
    $_SESSION['userID'] = $userID
         
    $_SESSION['lastLogin'] = $row['lastLogin']; 
         
    $updateLastLogin mysql_query
            
    "UPDATE users 
            SET 
            lastLogin = 
    $lastLogin 
            WHERE userID = 
    $userID 
            LIMIT 0,1"
    ); 
         
    header('Location:index.php'); 
         exit; 
         }
    }
    else 

         
    $_SESSION['loggedin'] = 0// Setting session var 'loggedin' to false 
         
    header('Location:error.php'); // Redirect to error page, as a matching record was not found in the table. 
         
    exit;  // always 'exit' 

    ?>
    You really need to get yourself some kind of IDE in order to avoid this problem. What do you use right now?

  3. #3
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh... that was the mistake. Everything works great now. I just had to tweak something in the UPDATE statement (problem with the LIMIT part). Thank you very much for your help.


  4. #4
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by streetlife
    You really need to get yourself some kind of IDE in order to avoid this problem. What do you use right now?
    ooops sorry, I didn't see this part. I am afraid I don't know what IDE means .
    I use PHPEdit to work on my pages if that can answer your question (I guess it does )

  5. #5
    SitePoint Addict streetlife's Avatar
    Join Date
    Mar 2004
    Location
    D-Town
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IDE means Integrated Deveopment Environment. The idea being that silly mistakes like not closing a tag etc. is pointed out to you before compiling stage, amongst a bunch more of other useful features. PHPEdit is an IDE, and a good one too, so you're alright! Glad you got things working.

  6. #6
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your explanations
    I missed the parse error in phpEdit, sure that I was the problem was coming from the double query.

    Thanks a lot for your positive attitude (too bad I have to wait to give you rep again )



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •