SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a simple form (form1) to add a record to the mysql database.
    When submitting one is redirected to form2.
    I form2 I need to obtain the id of the inserted record in form1.

    I tried mysql_insert_id on form2 but this always returns 0.

    Is there another way to obtain the id?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post the relevent code and tell us which versions of MySQL and PHP you are using (not that I know anything about version issues with either software - but someone else might).

    Otherwise my advice is to use mysql_insert_id()

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you trying to get the id, you need to use the database link to get it and You should really get it on the page you make the insert. Here is the way it works

    $link_id = mysql_connect("host", "user", "pass");
    mysql_select_db("dbname");
    $result = mysql_query("INSERT......");

    $lastid = mysql_insert_id($link_id);

    You need to refernece the db link identifier not the query identifier.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, you may not have to reference anything at all. I think I originally fell into the pit Freddy mentioned: I used the query variable ($result, or something like that) for the function's reference, and was dumbfounded when it didn't work.

    I ended up using something like this:

    PHP Code:
    $id mysql_insert_id(); 
    It, I believe, gives you the inserted ID number of the last query called - so if you stick it after your query, you shouldn't need to reference anything.

    Good luck.

  5. #5
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it works now!

    I placed the mysql_insert_id right after the insert statement and now it works.

    Before I had the mysql_insert_id further down the page and it didn't work then, it would always return the value 0.

    Thank you all for your help!

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, so I guess there are some stuff (queries) between the INSERT and mysql_insert_id()
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We'll never know because he didn't post his code!

  8. #8
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid
    We'll never know because he didn't post his code!
    Well I didn't post the code anymore, because I already solved the problem by moving the mysql_insert_id()
    to right below the INSERT query.
    Son Nguyen was right, i had a query in between the INSERT and mysql_insert_id() before.

    Regards,

    JamesBond


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
  •