#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    34
    Rep Power
    3

    How to insert an auto-generated ID of one table into another table's ID


    hi there,

    I am trying to make a project which is an Online shopping website. I have three tables,namely, Category Table, Sub-Category Table and Products table. In category table I have CategoryID and CategoryName as the two fileds.In Sub-Category table I have CategoryID, Sub-CategoryID and SubCategoryName as the fields, and in products table I have CategoryID, SubCategoryID , ProductID and ProductName.

    Now in category table , the categoryID is autogenerated and set as primary key.In sub-Category table, the none of the keys are auto generated and both the categoryID and subCategory ID are set to combined primary keys. Now ,when I insert a categoryName into the category table, I want the CategoryID(auto-generated) to be inserted in the categoryID column of the SubCategory table and the CategoryID column of the ProductTable. There is a lot of content in the net on this matter, but none solves exactly my problem.
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Run the following SQL after your first insert - but it, of course, depends on the database system you are using:

    Code:
    SELECT LAST_INSERT_ID()
    Last edited by AndrewSW; January 8th, 2013 at 05:21 PM.
    "The mysql extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used." the docs
  4. #3
  5. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Post All Connected World


    All Connected World।Evrey World Tips,Seo Tips,Haking Tips,Internet Marketing,Wordpress,Jomla,Software,Multimedia,Blogspot,Online Marketing,Online Income & Mane More.

IMN logo majestic logo threadwatch logo seochat tools logo