Let's say I have a MySQL database with a user table as follows:
|ID| Name| email |...
|1 | John| john@doe.com|...
I will have a php page where the user will select using checkboxes, between the various options in two independent areas of interest. In this example, we can say: favorite fruits and cars.
Fruits: apple, lemon, pear, banana. Cars: Ferrari, Mercedes, Volvo, BMW.
Now I have two options:
1.) Creating a column for each fruit and each car associated with each user, something like this:
|ID| Name| email |apple | lemon | pear | banana | ferrari | mercedes | volvo | BMW |
|1 | John| john@doe.com| 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
2) Create two new tables: fruits and cars.
|ID| Name |
| 1| Apple |
| 2| Lemon |
| 3| Pear |
| 4| Banana|
|ID| Name |
| 1| Ferrari |
| 2| Mercedes |
| 3| Volvo |
| 4| BMW |
Then create a user_fruit relational table and another called user_cars. For instance:
|ID_user| ID_fruit |
| 1 | 1 |
| 1 | 2 |
| 1 | 4 |
(Note that fruit id 3, Pear is not displayed because it is not selected → 0)
|ID_user| ID_car |
| 1 | 2 |
| 1 | 4 |
:
, ? , , tinyint.
, , , , php, .
, - .
- , , , , .
!