2016-12-06 82 views
0

嘗試設置動態html表單以將數組插入數據庫與pdo。 不添加新的輸入字段,它工作正常。只要我添加字段,我會收到錯誤,說我的數組是空的。php pdo動態html表單插入數組

PHP代碼:

if(isset($_POST['submit'])) 
{ 
    $items = (isset($_POST['items']) && is_array($_POST['items'])) ? $_POST['items'] : array(); 

    $insertStmt = $db->prepare("INSERT INTO products (prod_id, type) VALUES (:id, :name)"); 
    foreach ($items as $item) 
    { 
     $insertStmt->bindValue(':id', $item['id']); 
     $insertStmt->bindValue(':name', $item['name']); 
     $insertStmt->execute(); 
    } 
} 

jQuery代碼:

$(document).ready(function() { 
    var max_fields  = 10; 
    var wrapper   = $(".input_fields_wrap"); 
    var add_button  = $(".add_field_button"); 

    var x = 1; 
    $(add_button).click(function(e) 
    { 
     e.preventDefault(); 
     if(x < max_fields) 
     { 
      x++; //text box increment 
      $(wrapper).append('<div>Product ID<input type="text" name="items[][id]"/><br>Product Name<input type="text" name="items[][name]"/><a href="#" class="remove_field">Remove</a></div>'); 
     } 
    }); 

    $(wrapper).on("click",".remove_field", function(e) 
    { 
     e.preventDefault(); $(this).parent('div').remove(); x--; 
    }) 
}); 

的html代碼:

<form action="" method="POST"> 
    <div class="input_fields_wrap"> 
    <button class="add_field_button">Add Product</button> 
    <div>Product<input type="text" name="items[][id]"></div> 
    <div>Product Name<input type="text" name="items[][name]"></div> 
    </div> 
    <button type="submit" name="submit" class="inner">Save workout</button> 
</form> 

UPDATE:

因爲數組不允許我改變了相同的密鑰我的JavaScript。

$(wrapper).append('<div>Product ID<input type="text" name="items['+x+'][id]"/><br>Product Name<input type="text" name="items['+x+'][name]"/><a href="#" class="remove_field">Remove</a></div>'); 

也是表單輸入。

<div>Product<input type="text" name="items[0][id]"></div> 
<div>Product Name<input type="text" name="items[0][name]"></div> 
+0

它說你的數組是空的? 我建議在PHP的頂部嘗試'var_dump($ items)'來查看數組是否按照您期望的方式來到 – Joundill

+0

@Joundill Error1:未定義索引:名稱Erro2:完整性約束違規:1048列'name'不能爲null''insertStmt-> bindValue(':name',$ item ['name']);' – mypoint

+0

數組通過vardump傳入。 – mypoint

回答

1

默認情況下,PHP把輸入匿名數組到分隔的項目,這對你發佈的內容2個輸入,ID和名稱和PHP的原因解釋爲4個項目組成的數組,這樣你的foreach將循環4而不是2,而且你永遠無法在同一個循環中捕獲id和name。

最好的解決辦法是把和號碼每一個輸入羣組,如:

items[0][id] 
items[0][name] 

items[1][id] 
items[1][name] 

看到這個職位的進一步信息:How to POST data as an indexed array of arrays (without specifying indexes)

但是,有一個其他的方式才達到你希望沒有定義索引號,它只需要你反轉你輸入數組中的名稱,如下所示:

從:items[][id]到:items[id][]

和:items[][name]到:items[name][]

和你的PHP應該是:

if(isset($_POST['submit'])) 
{ 
    $items = (isset($_POST['items']) && is_array($_POST['items'])) ? $_POST['items'] : array(); 

    $array_ids = $items['id']; 
    $array_names = $items['name']; 
    $array_items = array_combine($array_ids, $array_names); 

    $insertStmt = $db->prepare("INSERT INTO products (prod_id, type) VALUES (:id, :name)"); 
    foreach ($array_items as $id => $name) {   
     $insertStmt->bindValue(':id', $id); 
     $insertStmt->bindValue(':name', $name); 
     $insertStmt->execute();   
    } 
} 

創建兩個數組,一個只包含IDS和只含名字一個方式,然後你將它們組合成一個數組,該數組將id放在鍵上並將其命名爲value。有了這個,你可以做你的foreach,並在一個循環中檢索id和名字。

+0

謝謝!它的工作原理,除了問題,如果數組的鍵相同。 如果我想輸入:[1] => keyboard [1] => case [2] => speaker' 它只會輸入'[1] => case' – mypoint