2011-02-23 114 views
2

我搜索了所有論壇,無法找到適合我的問題的答案,數組後處理對我來說這是相當新的。 我有作爲數組字段,處理陣列現場發佈數據

<input type="text" name="title[]" /> 
<input type="text" name="desc[]" /> 
<input type="text" name="cate[]" /> 

字段由PHP生成的HTML表單,我不知道有多少類似的數組字段會出現。當我輸出後我得到以下多維數組。

Array 
(
    [title] => Array 
     (
      [0] => title 1 
      [1] => title 2 
      [2] => title 3 
     ) 

    [desc] => Array 
     (
      [0] => description 1 
      [1] => description 2 
      [2] => description 3 
     ) 

    [cate] => Array 
     (
      [0] => cat 1 
      [1] => cat 2 
      [2] => cat 3 
     )) 

現在的問題是我想插入他們在數據庫中使用PHP的foreach。像..:

foreach($_POST as arraydata) 
{ 
    INSERT INTO sometable(title,desc,cata)VALUES(title[0],desc[0],cate[0]); 
} 

我嘗試了所有的招數,合併,連接,爆炸等,但我覺得有我probem得到類似下面沒有確切的解決方案,因此它可以很容易通過將其插入1 1謝謝:

(
    [0] => Array 
     (
      [title] => title 1 
      [desc] => description 1 
      [cate] => cat 1 
     ) 
    [1] => Array 
     (
      [title] => title 2 
      [desc] => description 2 
      [cate] => cat 2 
     ) 

    [2] => Array 
     (
      [title] => title 3 
      [desc] => description 3 
      [cate] => cat 3 
     )) 
+3

「我嘗試了所有的招數,合併,連接,爆炸等」 這聽起來像是你是一個魔術師的xD – 2011-02-23 11:17:38

+0

也許,這不用說,但一定要驗證和在將表單數據插入數據庫之前將其轉義。 – mesch 2011-02-23 14:31:37

回答

3
$title = $_POST['title']; 
$desc = $_POST['desc']; 
$cate = $_POST['cate']; 
for($i=1 ; $i < count($title) ; $i++) 
{ 
    INSERT INTO sometable(title,desc,cata)VALUES($title[$i],$desc[$i],$cate[$i]); 
} 
+0

呃容易打破堅果。謝謝你的解決方案。 – San 2011-02-23 11:27:12