Retrieve userid of logged in user for use to insert into another table
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Retrieve userid of logged in user for use to insert into another table

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    13

    Unhappy Retrieve userid of logged in user for use to insert into another table

    The title says it all. I've not found out how to do this. I have a form that only logged in users can fill in, however, I want to use the automatically created and auto-incremental userid from table 'users' to be used to identify WHICH user input the information in the form that inputs the same userid into a different table that doesn't auto_increment. Then I will also want to SELECT * FROM users where id = userid...however I need to know the code associated with retreiving and storing that userid as another $ .

    Please help...

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,854
    PDO has a LastInsertID() method, and (since you say "auto_increment" I'm going to guess you're using MySQL) the mysqli class has a $insert_id property.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    @Weedpacket I don't see how that will retrieve the userid associated with the user. From what I read about both the LastInserID() and $insert_id will retrieve the is associated with the last row INSERTED into a table. What I need is when WEEDPACKET logs in I want to call WEEDPACKET's userid so that when you input information into a form field that WEEDPACKET's userid is automatically entered with that information AND when I go to retrieve information from the database that ONLY WEEDPACKET's entered information is retreived.

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    When a user logs in, how do you authenticate them on subsequent page requests? Do you store some information in a session or a cookie?

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Ummm...neither...so, which would you recommend?

  6. #6
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    Quote Originally Posted by aarontbarksdale View Post
    Ummm...neither...so, which would you recommend?
    Session
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  7. #7
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    I'm confused... you initially said this:
    Quote Originally Posted by aarontbarksdale View Post
    I have a form that only logged in users can fill in
    That suggests that you have some way of determining whether or not a user has "logged in." How are you currently doing that?

    I would tend to agree with NogDog - using a session is often a good choice.

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Okay, so I DO have session in my check_login.php file (that I used tutorials to create). However, it only registers the username and password. How would I also add the userid? Is it as simple as session_register["userid"]; ?

  9. #9
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    If you're referring to the function session_register(), then no - you should never be using that function. Instead, you'd simply store the appropriate piece of data in the $_SESSION array.

  10. #10
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    Wherever you do the login DB query, make sure you also get the desired ID column in the SELECT part of the query, then add that value to the $_SESSION array along with the other values you want to keep in session. (I'm hoping you are using $_SESSION and not the long-deprecated session_register() function?)
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    I will use whatever is easier...whatever I can understand how they work...if $_SESSION is easier, then that's what I will use...I'm studying up on Sessions...but if you have a suggestion, then that will help my understanding.

  12. #12
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    okay...here's my thing, I get that sessions store info temporarily while the user is active. However, every person that registers for my site, my database will create a unique userid. So, BOB SMITH signs up and the next auto_increment number that is the userid is 500. However, I don't want BOB SMITH to be able to input something into the database that others can see, I need his specific userid to be pulled from the database upon login. Then I guess I can use SESSIONs to keep it stored for future usage. I need to know how to word the sql query to draw that out. SELECT userid FROM users WHERE username = "$username" ? If I set the $username in a session...will that work?

  13. #13
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    $_SESSION is both easy and not in danger of being unavailable in modern PHP installations. All you have to do is make sure each page that may want to read from or write to the session data has a session_start(); at the beginning, before anything gets output to the browser. Then you just access the $_SESSION array elements whenever you need them.

    page_1.php:
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['foo'] = rand(10009999);
    ?>
    <a href='page_2.php'>next</a>
    page_2.php:
    PHP Code:
    <?php
    session_start
    ();
    echo 
    "Your random number was: ".$_SESSION['foo'];
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  14. #14
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    something like

    $_SESSION['userid] = mysql_query ("SELECT userid FROM users WHERE username = $_SESSION['username']");

    provided that I define $_SESSION['username'] in my check_login.php file...is that what I'm understanding?

  15. #15
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,854
    Except that you'd want to (a) get the userid out of the query result set (you can't store the result set itself in a session - and incidentally, mysql_query is deprecated), (b) check that you did get a result, and (c) have a plan of what to do if you don't get a result.

    Usually you'd retrieve things like user id and user name from the database when you're looking at a login attempt and deciding whether or not it's successful. If it is, put the necessary data in the session (which would represent in the system the act of "logging the user in") and proceed with the application.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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