ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DB class saves logged in user as {username} instead of actual user name

June 19, 2008 6:00pm

Subscribe [2]
  • #1 / Jun 19, 2008 6:00pm

    applicode

    5 posts

    I am seeing a strange error when using the database class (global $DB).  When I try to use the EE tag {username} to save the current logged in user into the database (non EE table located in the EE database), the user is saved as {username}.

    When I echo the sql statement, the username is correct.  But the value being saved in the database is:  {username}

    It should be systemadmin.

    I have php parsing set to output.

    The user that is logged in is “systemadmin”

    Here is a sample of my code:

    global $DB; 
    
    $username = '{username}';
    
    $data = array(
        'FirstName' => $_POST['FirstName'], 
        'LastName' => $_POST['LastName'],                
        'ModifiedBy' => $username
        );
    
    print_r($data);
          
    $sql = $DB->update_string('Members', $data, "MemberID = 4584");    
    $DB->query($sql);        
    
    echo 'sql string: '.$sql;

    Here is the output from the print_r statement:

    Array ( [FirstName] => FirstTest [LastName] => LastTest [ModifiedBy] => systemadmin)

    Here is the output from the echo statement:

    sql string: UPDATE `Members` SET `FirstName` = 'FirstTest', `LastName` = 'LastTest', `ModifiedBy` = 'systemadmin' WHERE MemberID = 4584

    But when I view the database table, the ModifiedBy field is set to {username} and not systemadmin

    From the db table row:
    MemberID = 4584
    FirstName = FirstTest
    LastName = LastTest
    ModifiedBy = {username}  ==>  should be systemadmin

    Any ideas why is it saving it as {username} and not systemadmin?

  • #2 / Jun 19, 2008 6:35pm

    Ingmar

    29245 posts

    It’s a parse order issue. You should probably use

    $SESS->userdata['username']

    instead.

  • #3 / Jun 19, 2008 6:55pm

    applicode

    5 posts

    How can it be a parse issue if the print_r and echo $sql both show the correct username?

    Wouldn’t this code

    $sql = $DB->update_string('Members', $data, "MemberID = 4584");    
    $DB->query($sql);

    excute during the php parsing stage?  $sql has the correct value according to the echo statement.

    I was trying to avoid using $SESS.

  • #4 / Jun 19, 2008 7:00pm

    Ingmar

    29245 posts

    How can it be a parse issue if the print_r and echo $sql both show the correct username?

    I said parse order. I think the value of {username} is simply not available when the query is run.

    I was trying to avoid using $SESS.

    Why? EE makes use of it, so it’s there, and you are using PHP already anyway, so I fail to quite see the point. Also, I am moving this thread to Howto.

  • #5 / Jun 19, 2008 7:34pm

    applicode

    5 posts

    Thanks.

    Yes, parse order issue - sorry.

    The reason for avoiding $SESS is in case I go to a cookies only mode. I thought $SESS variable would not be available.

    Also, regarding the parsing order, it was my understanding that if PHP parsing stage was set to output, all the EE tags would be interpreted first and then the php code would run.  Is that true?

    In this case,

    $sql = $DB->update_string('Members', $data, "MemberID = 4584");    
    $DB->query($sql);

    should run after {username} is replaced with systemadmin in

    $username = '{username}';

    I enabled Display SQL Queries and it shows the query as

    UPDATE `Members` SET `FirstName` = 'FirstTest', `LastName` = 'LastTest', `ModifiedBy` = '{username}' WHERE MemberID = 4584

    but the echo $sql results in

    UPDATE `Members` SET `FirstName` = 'FirstTest', `LastName` = 'LastTest', `ModifiedBy` = 'systemadmin' WHERE MemberID = 4584
  • #6 / Jun 20, 2008 1:49am

    Ingmar

    29245 posts

    Yes, parse order issue - sorry.

    Yes. I am sorry, but that’s exactly the issue you’re running into. Standard Global Variables are parsed all together at the very end, just before final output is sent to the browser.

    The reason for avoiding $SESS is in case I go to a cookies only mode. I thought $SESS variable would not be available.

    The sessions class is available regardless of your method of authentication: cookies, sessions_ids or both.

    Also, regarding the parsing order, it was my understanding that if PHP parsing stage was set to output, all the EE tags would be interpreted first and then the php code would run.  Is that true?

    Mostly, yes.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases