SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    new entry in table - how do I find out it's ID?

    sorry, but "id" is too short a word for the search function. I saw a post on this last summer, but can't find it using "identifier", etc

    basically, if I do:
    Code:
      
    "INSERT INTO responses SET 
    	   submitter_dept='$deptname', 
    	   ip='$ipaddy',
    	   datetime='$stamp' ";
    how to I find out the ID assigned to this new entry?

    This is to allow for future UPDATEs in the same table for the same record a la:

    Code:
     
    "UPDATE responses SET
    	foo='$foo1',
    	bar = '$bar1'
    	WHERE id='$<the id>";

    This is an anonymous survey, so no 'userid' is being entered into this table, but I am using SESSIONs - if you think I should pass and use a SESSION id as a column entry- instead of referencing the row's autoincrement ID--- how do I grab that badboy?

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    duplicate

    &*^$#%@@ I hit preview, it gave me an error, now I see it posted and I did another double.

    oh well...

    Quote Originally Posted by hakalugi
    sorry, but "id" is too short a word for the search function. I saw a post on this last summer, but can't find it using "identifier", etc [img]images/smilies/frown.gif[/img]

    basically, if I do:
    Code:
     
    "INSERT INTO responses SET 
    submitter_dept='$deptname', 
    ip='$ipaddy',
    datetime='$stamp' ";
    how to I find out the ID assigned to this new entry?

    This is to allow for future UPDATEs in the same table for the same record a la:
    Code:
     
    "UPDATE responses SET
    foo='$foo1',
    bar = '$bar1'
    WHERE id='$<the id>";
    This is an anonymous survey, so no 'userid' is being entered into this table, but I am using SESSIONs - if you think I should pass and use a SESSION id as a column entry- instead of referencing the row's autoincrement ID--- how do I grab that badboy?

  3. #3
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_insert_id() will return the ID of the last inserted row.
    after the insert, use this
    PHP Code:
    <?
    $LastInsertedID 
    mysql_insert_id();
    $SQLStatement 'UPDATE responses SET foo=\'' $foo1 '\', '
                  
    'bar=\'' $bar1 '\' WHERE id=' $LastInsertedID
    mysql_query
    ($SQLStatement);
    ?>
    Last edited by sojomy; Mar 31, 2003 at 13:33.

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to run the mysql_insert_id() function after you've updated the database. Not before.

    -Helge

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, but he meant that code should go after the INSERT has been done, to be used in the UPDATE.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry.
    I should start reading the posts more carefully...

    -Helge

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It shouldn't matter is it's INSERT or UPDATE so long as the field is auto_increment (on mySQL anyways).


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
  •