2010-09-29 99 views
1

我正在嘗試爲正在處理的應用程序創建一個批量編輯頁面。該表包含各有三個可編輯字段的產品行。用codeigniter中的一個表單向數據庫提交多行

<tr> 
    <input type="hidden" name="id" value="10"> 
    <td>SKU<td> 
    <td>Product Name</td> 
    <td> 
    <select name="product_category" value="" tabindex="4"> 
     <option value="1">Category 1</option> 
     <option value="2">Category 2</option> 
    </select> 
    </td> 
    <td><input type="text" name="current_inventory" class="short" value="15" tabindex="5"></td> 
    <td><input type="text" name="inventory_alert" class="short" value="6" id="inventory_alert" tabindex="6"></td> 
</tr> 

每一行都可以進行編輯,並有一個提交頁面上的按鈕。我應該如何正確格式化,以便可以使用值更新數據庫中的每個條目?謝謝。

回答

2

你可以使用數組作爲表單名稱與PHP

<input type="text" name="product[current_inventory]" class="short" value="15" tabindex="5"> 
... 

當你處理表單,您可以使用

foreach($_POST['product'] as $product) { 

    $current_inventory = $product['current_inventory']; 
    // sql statement to update product 

} 
+0

這是有道理的,但是當我打印數組僅包含在最後一排桌子。我覺得我錯過了一些小事。 – 2010-09-30 01:41:07

+0

是的,你需要更新數組。如果你需要像我一樣擁有一個數組數組(更像是一個數據庫結果集,一個行數組是字段數組),那麼試試:'name =「product [i] [current_inventory]」'並增加我爲每一行。 – ise 2012-04-09 14:13:06

相關問題