2016-06-17 105 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