简体   繁体   中英

PHP MYSQL Advice for setting up a database project

I hope you can help. I'm a PHP/mySQL novice and trying to set up a personal site that displays photographs. Each photograph will have a type (such as nature or urban) and the navigation will be a list of the types. However, each photograph can have more than one type. For instance a photograph could be in both the nature and urban sections if it is a picture of an animal in the city).

I have got some distance with this project but I am not sure I am setting it up sensibly. I have each type set up as a database table field. For example I have a field for nature and one for urban and I set the row value to true if the photograph satisfies the criteria.

Another issue is I need to add new fields when new categories are encountered. I have set up an admin page to add more columns but this means all the INSERT commands etc have to be dynamic and it is causing me headaches.

Out of interest how would you far more seasoned developers approach this?

Thanks in advance for any help.

You can see that your solution won't scale - you have to keep adding columns, most of them will be NULL, etc.

I'd put a mapping table in between, like so:

photos -> photo_cat_x -> category

photo_cat_x might look like this:

  photo_id int(11) not null,
  category_id int(11) not null

Put one or more rows for each photo in photo_cat_x. When you want to find all the photos in a category, start in that new table.

Hope that helps!

You could have table for the photographs, a table for the types, and then a third table to define a many-to-many relationship between the first two tables.

This would make is much easier to dynamically add new types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM