在MySQL数据库中存储2D数组

时间:2016-06-17 22:08:14

标签: mysql

我正在尝试构建一个我需要将数据存储到MySQL数据库的项目。 对于每个活动,我可以拥有多个资源。与Activity One一样,我可以拥有Resource1,Resource2,Resource3等。

如何将这些资源存储在数据库的一个列中?

foreach($_POST['name'] as $key=>$value){
        //what do I insert?
        mysqli_query($conn,"INSERT INTO activity
            (`serial`,`Name`) VALUES(null,'$value')");
    }

现在,我只能保存活动的名称。

1 个答案:

答案 0 :(得分:1)

一般来说,你不应该;而是创建一个activity_resources表。该表可以允许多行引用相同的活动,并使用每一行来存储单独的资源。

activity:
- identifier field (often an auto-increment, but perhaps "serial" in this case)
- fields for "fixed" activity data, such as: name, perhaps date started;
  basically any values all activities are expected to have in fixed quantities.

activity_resources:
- activity identifier
- resource value|data

如果资源可以在多个活动之间共享,而是拥有resources表(保存资源),并使用activity_resources表来表示M:N关系;在这种情况下,表格的行将改为引用活动和资源表。

activity:
- identifier field (often an auto-increment, but perhaps "serial" in this case)
- fields for "fixed" activity data, such as: name, perhaps date started;
  basically any values all activities are expected to have in fixed quantities.

resource:
- identifier field
- resource value|data

activity_resources:
- activity identifier
- resource identifier