S
S
Sergey2015-05-18 02:59:51
MySQL
Sergey, 2015-05-18 02:59:51

What is the best way to implement combobox in database?

A combobox is a markup element. It looks like a drop-down list with the ability to enter your own value if nothing from the list came up.
What is the best way to implement such functionality? How to store new values ​​in the database?
I see several implementations, but I don't think any of them are perfect.
First option: "2 fields".
You need to choose a color when creating a new product for the catalog. There are 2 tables "Products" and "Colors". The product has a field "Color ID" and "Other color". In the product creation form, in fact, there are 2 fields - one allows you to select a color from the list and save its ID, the second field is just a text field, you can enter your color if the desired color is not in the list, and save it in "Other color".
This method requires checking that the product either has an ID or a different color.
The second option: "Auto-substitution".
Everything is the same, only the product has only one text field "Color". The dropdown list simply populates this field. Even after selection, you can edit the content, nothing will break. The choice will be saved in the product as text (for example, the content of the field will be "Blue").
With this method, it will be (nearly) impossible to filter products by color. But this option is suitable, for example, for chat or technical support to select standard answers.
Third option: "Create on the fly".
If we entered in the field an option that is not in the list, then it will automatically be added to the table. Accordingly, for our example, there will be only one field in the product table - "Color ID".
In this method, after active use, the drop-down list will contain a bunch of junk, typos, erroneous entries, and so on. But you can display entries added on the fly only after moderation. In general, this is the coolest option, but the most difficult.
Maybe there are better or easier options?
And by the way, if your version has an html-implementation (a plugin, for example), I will be glad to see it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fortop, 2015-05-18
@Fortop

This question has a strange relation to mysql, especially considering the request for an example in html ... There
are actually only three mechanisms.
1. Any user can enter new values.
2. Only certain people can edit the list by adding or removing values.
3. No one can change it, only choose from existing ones.
The choice of a particular option depends more on the business process.
That is, if all users are exclusively employees and there are few of them, then they can be allowed to work according to clause 1.
If there are many users (tens, hundreds, thousands), then it is most likely better than clause 2, when only a limited circle of responsible persons has the right for correction.
Point 3 is useful for cases where system users are not decision makers and/or there are a lot of them, and no one is monitoring the data separately. In this case, it is better to limit the available subset of choices, just to avoid garbage and mess in the data.
In all three cases, data of this kind must be stored in a separate table and links should be built with others by feature ID.
Plus, I recommend taking into account the ability to mark them as deleted while not physically deleting them from the database, so as not to be displayed in the list of selection options for users, but at the same time to have an adequate view in the archives of past operations, when this data could be selected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question