is it possible to post two datas in one column in mysql database? [closed]

懵懂的女人 提交于 2020-01-25 06:59:50

问题


What I want to do is I want to post two datas in one table but I don't know if it's possible or not. Is there any way that a column can hadle two datas or more at once?

Here is the modal

Like for example I have two monitors that I want for my unit. But from what I can see from the database only the first field gets recorded not the added field. As you can see I have a little function where I can add and delete fields in categories where items can be multiple or not.

Here is my table that I want to send the datas in


回答1:


You should really look into database normalization. With your current table structure, there's no efficient way to do what you need (because you only have one field for storing each type of component. A workaround would be to convert each field that could potentially hold multiple user selections to a JSON type and store them there. (all it takes would be to build a $monitors array and then convert it to json (with json_encode($monitors) prior to inserting. This, while feasible, has some drawbacks in terms of efficiency and indexability/searchability

what you really need is, like I said earlier, normalizing your tables.

You should have a "pivot" table (let's call it "assemblies") and separate component tables (call them what you want, but it could be "monitors", "cpus", "memory" etc). When assembling a new device, you'd create a new "assembly" and insert the components in their respective table with a reference to the master "assemblies" table By doing this, you could have a single assembly referencing multiple entries in the "monitors" table because this structure allows establishing "one-to-many" relationships

I strongly suggest you get your feet wet with normalization. Wikipedia could be a good starting point HERE

A more in depth explanation with examples can be found HERE




回答2:


You can store json object in json datatype and application level you can access



来源:https://stackoverflow.com/questions/58832410/is-it-possible-to-post-two-datas-in-one-column-in-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!