2016-06-17 103 views
-1

我正试图建立一个项目,我需要将数据存储到MySQL数据库。 对于每个活动,我可以有几个资源。就像活动一样,我可以有Resource1,Resource2,Resource3等。将二维数组存储在MySQL数据库中

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

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

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

回答

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