Jump to content

Inserting into database, getting unique id back?


Recommended Posts

I'm working on some recipe databse for a website and I'm in the planning states of how to set the thing up. Right now I figure I need a few different tables in my databse so it looks nice.

 

Table one would be the recipe listing, which has the name of the recipe, date put in, who posted it, instructions, and so on. Table two would have the ingredients, amounts, and little notes about them if needed.

 

What I'm trying to do is tie the multiple list of ingredients to the single listing of the recipe. From what I know I think I'll have to use a unique ID in the first table that is auto_incrementing. In the second table there will be an ID column as well, but it won't be unique to the table. That way, when a recipe is posted and it is the first one added it gets the ID of 1. The ingredients will then all be given an ID of 1 on the second table so that when I click the recipe to view it on an html page it will pull the ID 1 from table one and everything with ID 1 in the second table and put it all on my page.

 

But, I'm not sure how to go about doing this. The part that I'm unsure of is how to get the ID from table 1 out right after I've put that recipe listing in so that I can add that ID to all the ingredients listed in table two. I was planning on letting mySQL take care of incrementing and picking an ID for table 1. I was thinking that I would have to add the recipe to table 1, then turn around and do a select on the table with multiple values to get the exact listing and grab the ID at this point and add all the ingredients with the ID. But, that just seems to be a lot of extra work on the database and was hoping there was a better way about going at this.

 

Any help is appreciated, or any idea of a new way to make the database and tables would work too. Thanks!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.