tags:

views:

40

answers:

2

I was wondering how can I have my users pick their favorite food from a category of foods in my MySQL database.

Will I need two different tables? If so what will my second table look like?

Here is my MySQL food table structure.

id | parent_id | food | url
+5  A: 

You'll need 3 tables in total:

  1. Food - holds food information
  2. Users - holds users information
  3. Users_Food - holds user id + food id (and maybe a ranking)

You should probably read up on database normalization.

Greg
Or since it's favourite food, that implies that they can choose one food (their favourite). In this case, it's not horrible to tack it on as another field in the users table.
Matthew Scharley
True if it's limited to one favourite
Greg
A: 

You'd need to make a second table:

user_id | food_id

Make them both primary. Then you can use JOIN's to select the food:

SELECT f.food, f.url
FROM user_food AS u 
INNER JOIN food AS f ON (f.id = u.food_id)
WHERE u.user_id = {USER_ID}

This will give you a list of all favorite foods set by the user.

Bart S.