SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    copying row data into another table

    Hi.

    I've got a query that inserts data into a table.

    Query1
    Code:
    "INSERT INTO `dbPosts` (`post`, `username_id`, `private`) VALUES ('$post', '$user', '1')";
    When data is inserted it automatically creates an ID (incremented value) in the table row

    I've got another query in the same php document that inserts data into another table:

    Query2
    Code:
    "INSERT INTO `dbPrivate` (`username_id`) VALUES ('$user')";
    How do I copy the ID which was automatically inserted in query1 into query2.

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    You need the mysql_insert_id function. It will tell you the ID of the row you just inserted.

    So, insert the row in the first table, use mysql_insert_id to get that row's ID, and then insert the data into the second table.


    Edit>>
    I'm assuming you're using PHP for this ('cos of the $user variables in your SQL). If you're not then the mysql_insert_id function in PHP won't help much

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, would this still work if multiple people were using the query?

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,027
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    You're already inserting the same id into both tables. Is there something missing from query 2?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    I'm assuming you're using PHP for this ('cos of the $user variables in your SQL). If you're not then the mysql_insert_id function in PHP won't help much
    No but the LAST_INSERT_ID function in MySQL might. See the PHP manual where it warns you of possible errors in the use of mysql_insert_id and switch to LAST_INSERT_ID directly in the mysql query.


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
  •