#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    2
    Rep Power
    0
    Hi
    I have a set of data that is being inserted into two tables but i want to catch the new ID (auto incremented) of the first table and place its value in a field with the second part of the data in the second table.

    how do i do this with PHP & MYSQL?
    And how do i ensure that another set of data hasn't ben inserted in the first table meantime?

    I want do do something like - but dont know correct syntax or even if it could ever work.

    INSERT INTO content (title,description,typeid,userid,dateadded) VALUES ('$title','$description','$typeid','$userid','$date')

    then

    INSERT INTO links (url,sitename,contentid) VALUES ('a',b',SELECT LAST_INS
    ERT_ID() FROM content);

    your expert help would be greatly apreciated to help me understand this stuff

    Thanks
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    Do your first insert like you've got, then

    $last_id = mysql_insert_id($link_id);

    to get the number of the last id. then do the insert into your second table, using $last_id where ever you need it. You don't need the $link_id if you only have a connection open to one database.

    ---JH

    ------------------
    *************************************************************
    * The manual can probably answer 90% of your questions... *
    * *
    * PHP Manual. www.php.net/manual *
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter *
    *************************************************************
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    2
    Rep Power
    0
    Thanks, That is a great help.

    I guess I was RT WRONG FM ;-) - will check both next time.

    Angus
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    Actually, you can do it in two sql statements also, I just gave it a try...

    INSERT INTO content (title,description,typeid,userid,dateadded) VALUES ('$title','$description','$typeid','$userid','$date')

    INSERT INTO links (url,sitename,contentid) VALUES ('a',b',LAST_INSERT_ID());

    That's probably more what you were looking for...

    ---JH

Similar Threads

  1. insert and update 2 tables
    By welly in forum PHP Development
    Replies: 14
    Last Post: February 23rd, 2004, 07:41 AM
  2. cannot insert data
    By lmy52 in forum PHP Development
    Replies: 24
    Last Post: February 14th, 2004, 10:12 AM
  3. Replies: 4
    Last Post: February 8th, 2004, 11:07 PM
  4. display data from two tables using drop down box
    By skootz in forum PHP Development
    Replies: 9
    Last Post: January 23rd, 2004, 08:55 AM
  5. Replies: 1
    Last Post: January 19th, 2004, 11:32 PM

IMN logo majestic logo threadwatch logo seochat tools logo