简体   繁体   中英

Select with php and mysqli

I have this in my database:

table styles

style_id    style
1           rock
2           blues
3           jazz
etc...

table user_styles

user_id    style_id
1          1
1          2
2          3
etc...  

I want do this:

  • If user_styles for user_id(ej. 1) is empty then show the table styles in a select for user to select. If is not empty, show the results for the user_id (ej. 1).
  • Insert/update the options selected into de table user_styles.

1: Check for s.style for a particular user using below mysql query: "SELECT style FROM styles s JOIN user_styles us ON s.style_id = us.style_id WHERE us.user_id = ?, [UserId]"

2: if you are getting no value, then go for showing all styles present in style table in a dropdown, allow user to select and submit. onSubmit, insert the style detail into user_style table. 3: else show the style for the user.

This is the logic best comes to my mind now. You can also try many other ways as well. I do not see any logical or programatical issue here.

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