2017-03-11 58 views
1

這是某個站點的管理面板的代碼。客戶提出的約會將顯示在此頁面中。管理員將能夠根據可用性change約會。提交由循環生成的表單的特定行

<?php 

while($row = mysqli_fetch_assoc($result)){ 

?> 

<form action="adminEdit.php" method="POST"> 
<tr> 

    <td><input type="text" id="name" value="<?php echo $row['Name'];?>"></input></td> 
    <td><input type="text" id="address" value="<?php echo $row['Address'];?>"></input></td> 
    <td><input type="text" id="phone" value="<?php echo $row['Phone'];?>"></input></td> 
    <td><input type="text" id="license" value="<?php echo $row['Car_License_No'];?>"></input></td> 
    <td><input type="text" id="engine" value="<?php echo $row['Car_Engine_No'];?>"></input></td> 
    <td><input type="text" id="date" value="<?php echo $row['Date'];?>"></input></td> 
    <td><input type="text" id="mechanic" value="<?php echo $row['Mechanic'];?>"></input></td> 

    <td><input type="submit" name="submit" value="Change"/></td> 

</tr> 
</form> 

<?php 

} 

?> 

在這裏,每行數據都有一個對應的按鈕,它將用於更改或修改該特定行的記錄。一旦管理員更改了特定的約會,它應該在數據庫中更新。

我的問題是,所有的行都是通過while循環生成的。現在如何訪問特定行,當點擊該特定行的change按鈕時?當行由循環產生時,我贏得了t be able to access them by名稱or id because all of them will have the same名稱and id`。

搜索了相關問題,但沒有一個與我的場景匹配。這將有助於獲得答案。提前致謝。

回答

0

就我個人而言,我傾向於從循環中獲取輸出以更好地控制數據並解決問題。您還在每個循環中創建一個新窗體。
只需循環DB並創建一個新變量,然後使用該變量輸出表單中的數據。

示例代碼顯示的基本理念,而不是測試或說明它是完整的等:

while ($row = mysqli_fetch_assoc($result)) { 
    $someNewVar[$row['id']] = $row; 
    // The 'id' index would be from DB which identifies individual rows 
} 
?> 

<form action="adminEdit.php" method="POST"> 

<?php 
foreach ($someNewVar as $index => $value) { 
?> 
<tr>  
    <td> 
    <input 
     type="text" 
     id="<?php echo $index;?>" 
     value="<?php echo $value['Name'];?>"> 
    </input> 
    </td> 
    <td> 
    <input 
     type="submit" 
     name="submit" 
     value="Change"/> 
    </td> 
</tr> 

<?php 
} 
?> 
</form> 

然後,你需要有從點擊提交按鈕傳遞的行ID。


在一個側面說明,這整個方法可收拾,而且數據應在一個單獨的文件到您將它輸出來獲得。 然後在獲取數據的文件中,您可以將數組設置爲在輸出文件中也標識了的數據,以便在沒有數據獲取時進行管理。

訪問getdata.php

while ($row = mysqli_fetch_assoc($result)) { 
    $dataFromDb[$row['id']] = $row; 
} 
$someNewVar = !empty($dataFromDb) ? $dataFromDb : false; 

showData.php

if ($someNewVar) { 
    // do the loop and form 
} else { 
    echo 'sorry no data found'; 
} 
+0

非常感謝你爲你準確的答案。只是問,如果我在每個輸入中放置一個隱藏值,是否可以有一種方法? –

+0

您可以將隱藏值放入表單(每行)的每個循環中,例如ID併發送提交。 – James