2015-05-02 49 views
0

嘗試從mysql查詢創建表單,我可以在其中更新每行的字段。但它只是發佈我的最後一行。 這是我的。無法基於mysql查詢獲取表單來加載所有POST變量

require('includes/application_top.php'); 
    if (!empty($_POST['inventory_daily-submit'])) { 
    foreach($_POST['id'] as $id) { 
    print_r($_POST);} 
    } ?> 
    <form name="inventory_daily" method="post"> 
    <table> 
    <tr> 
    <td>Product #</td> 
    <td>Product Name</td> 
    <td>Count Unit</td> 
    <td>Begining Quantity</td> 
    <td>Ending Quantity</td> 
    <td>Unit Price</td> 
    </tr> 
    <input name="id[]" type="hidden" size="3" maxlength="3"> 
    <?php 
    $inventory = $db->Execute('select product_id, product_name, quantity_on_hand, count_unit, case_price, unit_price, active, category_id from ' . TABLE_INVENTORY . ' WHERE active = 0 order by category_id ASC'); 
    while(!$inventory->EOF) { 
    ?> 
    <tr> 
     <td><?php echo $inventory->fields['product_id']; ?><input name="product_id" type="hidden" size="3" maxlength="3" value="<?php echo $inventory->fields['product_id']; ?>"></td> 
     <td><?php echo $inventory->fields['product_name']; ?></td> 
     <td><?php echo $inventory->fields['count_unit']; ?></td> 
     <td><?php echo $inventory->fields['quantity_on_hand']; ?> 
     <input name="old_quantity" type="hidden" size="7" maxlength="7" value="<?php echo $inventory->fields['quantity_on_hand']; ?>"></td> 
     <td><input name="<?php echo "new_quantity_on_hand"; ?>" type="text" size="7" maxlength="7"></td> 
     <td><?php echo $inventory->fields['unit_price']; ?> 
     <input name="unit_price" type="hidden" size="7" maxlength="7" value="<?php echo $inventory->fields['unit_price']; ?>"> 
     <input name="case_price" type="hidden" size="7" maxlength="7" value="<?php echo $inventory->fields['case_price']; ?>"></td> 
     </tr> 
     <?php 
    $inventory->MoveNext(); 
    } 

?> 
    </table> 
<input type="submit" name="inventory_daily-submit"> 
</form> 

窗體顯示3行。

1 cheerwine每33 ____ 0.38
2涼爽藍色各11 ____ 0.66
3飲食PEPS每13 ____ 0.51

每一行我輸入new_quantity_on_hand以編號1少比old_quantity

所以我有3行應該與數字32,10和12下劃線,但只有最後一行的職位。

輸出是:

Array ([id] => Array ([0] =>) [product_id] => 3 [old_quantity] => 13.0 [new_quantity_on_hand] => 12 [unit_price] => 0.51 [case_price] => 12.25 [inventory_daily-submit] => Submit Query) 
+0

你的結果(例如,如果你檢查在瀏覽器中生成的源代碼),將具有相同名稱的'new_quantity_on_hand'三個字段。如果你發佈,只有最後一個元素將被提交(每個新的覆蓋之前)。 'old_quantity'也​​是一樣。 我通常建議你看看生成的代碼。這可能會幫助你看到其餘的錯在哪裏。 – Burki

回答

0
if (!empty($_POST['product_id'])) { 
     $date = date('Y-m-d'); 
     $i=0; 
     $m=count($_POST['product_id']); 
     echo $m; echo "<br>"; 
     while ($i < $m) { 
     if(!empty($_POST['new_quantity_on_hand'][$i])){ 
    $old = $_POST['old_quantity'][$i]; 
    $new = $_POST['new_quantity_on_hand'][$i]; 

     $sql_data_array = array('product_id' => $_POST['product_id'][$i], 
      'quantity_on_hand' => $_POST['new_quantity_on_hand'][$i],); 

     } 
     echo "Information has been successfully saved!<br>"; 
     } 
     $i++; 
     } 

     }