2013-10-14 94 views
0

因此,我創建了一個管理頁面,該頁面根據我們數據庫中有多少用戶創建了X個表單,每個用戶都有一個提交按鈕,提交對我們在數據庫中的日期的更改。我的問題是,當我想獲得張貼的價值時,我無法從張貼的東西中準確提取我需要的東西。它會保存到一個數組打來電話,當我print_r的數組我得到正是我想要的,那就是:嘗試使用動態創建的表單更新數據庫中的日期

[1] => "whatever date they typed in" 
    (obviously the 1 changes depending on which item they changed the date of) 

我需要能夠通過查詢我的DATEBASE:

UPDATE users SET subdate="whatever they typed in" WHERE id="the array reference number" 

我知道到底我需要做的,我只是不熟悉SQL,因爲任何幫助將不勝感激。提前致謝。

參考

代碼:

<div class="form-section grid12" id="changedates"> 
<h1>Change Dates</h1> 
    <?php 
      $query = mysql_query("SELECT * FROM users WHERE admin='y'"); 
    ?> 
    <table> 
    <?php 
     while($row = mysql_fetch_assoc($query)) { 
    ?> 
    <tr> 
     <td> 
     <h5><?php echo $row['displayname'];?></h5> 
     </td> 

     <td> 
     <form action="" method="POST"> 
      <input type="text" name="subdate[<? echo $row['id'] ?>]" value="<?php echo $row['submissiondate'];?>"> 
      <input type="text" name="nextupdate[<? echo $row['id'] ?>]" value="<?php echo $row['nextupdate'];?>"> 
     </td> 

     <td> 
      <input type="submit" value="Set Date" name="setdate"> 
     </form> 
     </td> 
    <?php 
    } 
    ?> 
    </table> 

</div> 

回答

0

你可以使用的foreach ...

foreach ($_POST[nextupdate] as $rowId => $time) 
    { 
    // do db update 
    } 

編輯:剛剛意識到你有每個表單一個以上的輸入。

爲什麼不命名以與陣列名稱每個輸入:

<input type="text" name="form_data[<?= $row_id ?>][subdate]"> 
<input type="text" name="form_data[<?= $row_id ?>][nextupdate]"> 

在PHP:

foreach ($_POST[form_data] as $rowId => $values) 
    { 
    $subdate = $values[subdate]; 
    $nextupdate = $values[nextupdate]; 

    // do SQL stuff 
    } 
相關問題