简体   繁体   English

MYSQL在一行或多行中存储输入值

[英]MYSQL storing input values in one row or multiple rows

I'm working with PHP and MySQL, and I'm not too experienced with database design. 我正在使用PHP和MySQL,并且对数据库设计没有太多的经验。 I know the basics of normalization, however my question is about storing vales from a specific form I have. 我知道规范化的基础知识,但是我的问题是关于从我拥有的特定形式存储值。

Ok, so the way my page in question is set up, there are text boxes that can be dynamically added or removed. 好的,这样我的页面设置方式就是存在可以动态添加或删除的文本框。 If the user presses a "+" or "-" button it adds or removes another text box. 如果用户按下“ +”或“-”按钮,则会添加或删除另一个文本框。

Anyway, when I pull the text box values via $_POST it is an array, and then I convert it to a string using implode() Ex: user enters "hello" "world" "bye" "world", implode converts it to the following string "hello,world,bye,world". 无论如何,当我通过$ _POST拉出文本框值时,它是一个数组,然后使用implode()将其转换为字符串例如:用户输入“ hello”“ world”“ bye”“ world”,implode将其转换为以下字符串“ hello,world,bye,world”。

My question is would it be better to store this string "hello,world,bye,world" in 1 row as 1 entry, or would it be better to break this string into 4 separate strings and store it in 4 different rows? 我的问题是将字符串“ hello,world,bye,world”存储为1行更好,还是将字符串拆分为4个单独的字符串并存储在4行中更好? Each text box will contain questions/comments so they could be potentially long and most likely not 1 word like the example I gave. 每个文本框将包含问题/评论,因此它们可能很长,很可能不是我所举示例中的1个字。

The reason why I'm asking this is because all of these inputs will be used on the same page, so they can all share a unique ID. 我之所以这样问,是因为所有这些输入都将在同一页面上使用,因此它们都可以共享唯一的ID。 Instead of querying multiple times, I'd only have to query once. 无需多次查询,我只需要查询一次。

They should be stored separately, because they're separate examples of one kind of data. 它们应该分开存储,因为它们是一种数据的单独示例。

Your table would be (for example) comments , and each row should be one comment, so one row for hello , one for world , etc. 您的表将是(例如) comments ,并且每一行应为一个注释,因此一行用于hello ,一行用于world ,等等。

You could also store a page ID alongside the comments, so your table would look like this (note that you'd typically have an id field for comment id, as well. I've omitted it for brevity): 您还可以在评论旁边存储一个页面ID,因此您的表格应如下所示(请注意,您通常也有一个id字段作为评论ID,为简洁起见,我将其省略):

|--------------|---------|
| comment      | page_id |
|--------------|---------|
| hello        | 1       |
| world        | 1       |
| bye          | 2       |
|--------------|---------|

Then you can still search on page_id, but your data is logically separated, and you can search on the specific comments if you wish. 然后,您仍然可以在page_id上​​搜索,但是数据在逻辑上是分开的,并且您可以根据需要搜索特定的注释。

SELECT * FROM comments WHERE page_id = 1 -- 2 results

You can also do things like ordering it alphabetically, or by time (with a new column for that) very easily. 您还可以轻松地执行诸如按字母顺序排序或按时间排序(为此使用一个新列)之类的操作。

|--------------|---------|------------|
| comment      | page_id | time_made  |
|--------------|---------|------------|
| hello        | 1       | 1234567890 |
| world        | 1       | 2356480546 |
| bye          | 2       | 5168161543 |
|--------------|---------|------------|

SELECT * FROM comments WHERE page_id = 1 ORDER BY time_made DESC -- most recent first

最好将此字符串分成4个单独的字符串,并将其存储在单独的子表的4个不同的行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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