2015-10-14 31 views
-3

我想將數組傳遞到數據庫中,但無法正確傳遞數組。 我有以下DB行如何將數組傳遞到數據庫

Country, number, Page, date 

並希望在我的數據庫是這樣的

id Country number Page date 
2  Sweden 5  cat 14th june 2015 
     United 10  ind 14th june 2015 
     states 58  con 14th june 2015  
     France 101  aces 14th june 2015  
     Germany 200  ind 14th june 2015 

我用這

if(isset($_POST['submit'])){ 
$new_res = mysql_query("INSERT INTO `em-res_db` VALUES('','$Country','$number','$Page','$date')"); 
       if ($new_res){ 
        //continue 
    } 

我希望你能得到什麼我的圖片試圖做

+0

我想你必須環路'POST' –

+0

1)*** ***停止使用*** ***過時'mysql'擴展,切換到'而不是「PDO」或「mysqli」。 2)瞭解[注入](http://bobby-tables.com),並準備好陳述。 3)使用預先準備好的語句,並使用一組值來執行它們。 3b)如果'id'是你的PK和'AUTO_INCREMENT'字段,那麼不要插入一個空字符串作爲值,只需指定你實際設置的字段,然後讓DB處理自動增加的內容。 –

+0

數據庫連接在哪裏?它產生了什麼錯誤? –

回答

0

創建一個與數組長度相同的循環,並在循環中將代碼插入數據到數據庫的位置。

for(i=0;i<=count(array)-1;i++){ 
if(isset($_POST['submit'])){ 
$new_res = mysql_query("INSERT INTO `em-res_db` VALUES('','$Country','$number','$Page','$date')"); 
       if ($new_res){ 
        //continue 
    } 
} 
相關問題