How to retrieve a loggedin user from MySQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naira
    New Member
    • Apr 2010
    • 4

    How to retrieve a loggedin user from MySQL database

    Dear all,

    Below here I show you a redirected page from a login.php where shows the results os the login session.
    This page will be once again redirected to an edit_user.php page.

    The question is how do I retrieve this loggedin user_id from the database.

    I have already tried GET_uid and many other forms of request without success.

    I would be GREATFUL if any one could help me by that ! THANKS A LOT!


    Code:
    if (isset($_SESSION['first_name']) && ($_SESSION['last_name' ]) && ($_SESSION['email']) && ($_SESSION['user_id']) && ($_SESSION['registration_date']) )  { 
    ?><td><?php  
       echo "<br /><br /><fieldset><p>Welcome, {$_SESSION['first_name']} {$_SESSION['last_name']}!</p>              
       <p>E-mail address: {$_SESSION['email']}</p>
       <p>Registration date: {$_SESSION['registration_date']}</p>
       <p>Your ad...(s)</p> 
       <p>User_id: {$_SESSION['user_id']}</p>";
     echo "</fieldset  >";
      
    }
    
    if (isset($_GET['uid'])) {
    $uid = (int) $_GET['uid'];
    if ($uid > 0) {
     $query = "SELECT user_id AS $uid FROM users WHERE user_id = $_SESSION[user_id]" ;      
        $result = @mysql_query ($query);
        
    echo '<table> 
          <tr>
           <td align="left"><a href="edit_user.php?view= ' . $SESSION['user_id'] . '">Edit</a></td>
           <td align="left"><a href="delete_user.php?view= ' .  $SESSION['user_id'] . '">Delete</a></td>
         </tr>
        '
    ; '</table>' ; 
      
    } 
     mysql_close();
      }

    THANK YOU VERY MUCH ! Naira
    Last edited by Atli; Apr 26 '10, 05:53 PM. Reason: Fixed the [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Is the session variable not working?
    The "$SESSION['user_id']" element that is used several time in that code, I mean.

    Comment

    • phpuser123
      New Member
      • Dec 2009
      • 108

      #3
      Try to echo ur select statement,check if the sql is ok ....
      Most probably da problem is somewhere on the sql mostly in line 15..
      What does it give u?

      Comment

      • Naira
        New Member
        • Apr 2010
        • 4

        #4
        Originally posted by Atli
        Hey.

        Is the session variable not working?
        The "$SESSION['user_id']" element that is used several time in that code, I mean.
        Hi Atli,

        The $_SESSION is working to loggin the user e show the user his name, email, etc, but not to identify him as a retrieved user_id on the page. Nothing else shows up besides the profile of the user.

        Many thanks.

        Naira

        Comment

        • Naira
          New Member
          • Apr 2010
          • 4

          #5
          Originally posted by phpuser123
          Try to echo ur select statement,check if the sql is ok ....
          Most probably da problem is somewhere on the sql mostly in line 15..
          What does it give u?
          hi phpuser123,

          Thanks for your help.
          The sql is running without problems. I have other exercices and everything is doing properly.
          I will try yr hint. Meanwhile I found an strong example online from an anonymous and I will try that too tomorrow.

          Many thanks
          Naira

          Comment

          • rythmic
            New Member
            • Feb 2010
            • 29

            #6
            The problem is the AS statment in the sql query.

            AS is used to create an alias of a table name or column name. Say you had a table called very_extremly_l ong_col_name it is much more convenient to reference this as velt

            so you would write

            Code:
            SELECT very_extremly_long_col_name AS c1 FROM some_table WHERE c1=3
            
            // Then the result would be delivered like this in your php code
            $result['c1'];
            
            
            // instead of  if not using AS
            SELECT very_extremly_long_col_name  FROM some_table WHERE very_extremly_long_col_name=3
            
            // imagine getting data from several long named tbls and column and you'll see its a beneficial feature
            So what you need to do is get rid of the AS statement in the sql.

            Comment

            • rythmic
              New Member
              • Feb 2010
              • 29

              #7
              Originally posted by Naira
              Dear all,

              Below here I show you a redirected page from a login.php where shows the results os the login session.
              This page will be once again redirected to an edit_user.php page.

              The question is how do I retrieve this loggedin user_id from the database.

              I have already tried GET_uid and many other forms of request without success.

              I would be GREATFUL if any one could help me by that ! THANKS A LOT!


              Code:
              if (isset($_SESSION['first_name']) && ($_SESSION['last_name' ]) && ($_SESSION['email']) && ($_SESSION['user_id']) && ($_SESSION['registration_date']) )  { 
              ?><td><?php  
                 echo "<br /><br /><fieldset><p>Welcome, {$_SESSION['first_name']} {$_SESSION['last_name']}!</p>              
                 <p>E-mail address: {$_SESSION['email']}</p>
                 <p>Registration date: {$_SESSION['registration_date']}</p>
                 <p>Your ad...(s)</p> 
                 <p>User_id: {$_SESSION['user_id']}</p>";
               echo "</fieldset  >";
                
              }
              
              if (isset($_GET['uid'])) {
              $uid = (int) $_GET['uid'];
              if ($uid > 0) {
               $query = "SELECT user_id AS $uid FROM users WHERE user_id = $_SESSION[user_id]" ;      
                  $result = @mysql_query ($query);
                  
              echo '<table> 
                    <tr>
                     <td align="left"><a href="edit_user.php?view= ' . $SESSION['user_id'] . '">Edit</a></td>
                     <td align="left"><a href="delete_user.php?view= ' .  $SESSION['user_id'] . '">Delete</a></td>
                   </tr>
                  '
              ; '</table>' ; 
                
              } 
               mysql_close();
                }

              THANK YOU VERY MUCH ! Naira
              Also if you didn't notice or if it is a simple typo you have used $SESSION in your delete and edit links instead of $_SESSION which you had correctly used in your sql statement.

              Comment

              • Naira
                New Member
                • Apr 2010
                • 4

                #8
                Originally posted by rythmic
                The problem is the AS statment in the sql query.

                AS is used to create an alias of a table name or column name. Say you had a table called very_extremly_l ong_col_name it is much more convenient to reference this as velt

                so you would write

                Code:
                SELECT very_extremly_long_col_name AS c1 FROM some_table WHERE c1=3
                
                // Then the result would be delivered like this in your php code
                $result['c1'];
                
                
                // instead of  if not using AS
                SELECT very_extremly_long_col_name  FROM some_table WHERE very_extremly_long_col_name=3
                
                // imagine getting data from several long named tbls and column and you'll see its a beneficial feature
                So what you need to do is get rid of the AS statement in the sql.
                Hello Rythmic,

                Sorry for the delay, I was on vacation.
                Thank you very much for your suggestion, but the solution was simply this one below:

                Code:
                 
                if (isset($_SESSION['user_id']) AND
                (substr($_SERVER['PHP_SELF'], -10)
                != 'logout.php')) {
                     
                echo '<br /><table> 
                      <tr>
                       <td align="left"><a href="edit_user.php">Edit</a></td>
                       <td align="left"><a href="delete_user.php">Delete</a></td>
                     </tr>
                    ';
                echo "</table>"; 
                  
                } ]
                Nothing more than that.

                Thanks any way!
                Have a nice weekend.

                Naira

                Comment

                Working...