Need help with server administration?
RSS icon Email icon Home icon
  • How to use get last inserted ID for multiple table?

    Posted on May 27th, 2009 servbit2 No comments

    Hi,
    How do get the last inserted ID from m_menu_1 to be inserted (as menu_info_id) to m_menu_2 and m_menu_3?

    The code below, only m_menu_2 is getting the last inserted ID from m_menu_1. Whereas m_menu_3 is getting the last inserted ID from m_menu_2.

    Please advise.

    SQL below
    ==================
    INSERT INTO m_menu_1(title, summary, details, created_by, created_date ) VALUES(strTitle, strSummary, strDetails, strUsername, NOW());
    INSERT INTO m_menu_2(menu_info_id, filename, created_by, created_date) VALUES(LAST_INSERT_ID(), strFilename, strUsername, NOW());
    INSERT INTO m_menu_3(menu_info_id, video_url, created_by, created_date) VALUES(LAST_INSERT_ID(), strVideoUrl, strUsername, NOW());

    Try this,.,.

    INSERT INTO m_menu_1(title, summary, details, created_by, created_date ) VALUES(strTitle, strSummary, strDetails, strUsername, NOW());

    SET @code = LAST_INSERT_ID();

    INSERT INTO m_menu_2(menu_info_id, filename, created_by, created_date) VALUES(@code, strFilename, strUsername, NOW());
    INSERT INTO m_menu_3(menu_info_id, video_url, created_by, created_date) VALUES(@code, strVideoUrl, strUsername, NOW());

    Leave a reply

    You must be logged in to post a comment.