2017-07-17 71 views
0

我動態地從一個DATABSE(所有HTML未包括爲了簡潔)填充表:
輸入字段提交最後一個索引,而不是索引選擇

foreach($WOaccountsInfo as $WOInfo){ 
     $WOT .="<tr>"; 
     $WOT .="<td>$WOInfo[1]</td>"; 
     $WOT .="<td>$WOInfo[2]</td>"; 
     $WOT .="<td>$WOInfo[3]</td>"; 
     $WOT .="<td>$WOInfo[4]</td>"; 
     $WOT .="<td><form method='post' action='/radiosite/other/index.php'>"; 
     $WOT .="<input type='hidden' value='$WOInfo[0]' name='ID' id='ID'>"; 
     $WOT .="<button type='submit' title='Edit'>Edit</button>"; 
     $WOT .= "<button type='submit' title='Delete' name='action' value='delWOEntry' onclick='confDel()'>Delete</button></td>"; 
     $WOT .="</tr>"; 
     } 

結果發送它這樣控制器:

$ID = filter_input(INPUT_POST, 'ID', FILTER_SANITIZE_NUMBER_INT); 
$result = delWOentry($ID); 
     if ($result === 1){ 
      header("Location: /radiosite/other/index.php?action=first&message=<p class='message'>The entry was successfully deleted from the database</p>"); 
      exit; 
     } 
      else { 
      header("Location: /radiosite/other/index.php?action=wopass&message=<p class='message'>Something went wrong with the deletion</p>"); 
      exit; 
      } 

刪除功能的代碼是在這裏:

function delWOentry($ID) { 
    $db = byuidahoradioconnect(); 
    $sql = 'DELETE FROM wideorbitaccounts WHERE ID = :ID'; 
    $stmt = $db->prepare($sql); 
    $stmt->bindValue(':ID', $ID, PDO::PARAM_INT); 
    $stmt->execute(); 
    $rowsChanged = $stmt->rowCount(); 
    $stmt->closeCursor(); 
    return $rowsChanged; 
} 

它確實刪除了一行......但它所做的是刪除SQL表中的最後一行。
在網頁中,我將輸入類型從隱藏改爲了文本,並且在頁面上它顯示了正確的ID號,但是當我將它發送給控制器進行處理時,變量顯示了ID號數據庫中的最後一行。

所以,簡而言之,我試圖根據行的ID刪除表中的一行,但它將刪除表中的最後一行而不是所選的行。

回答

1

您永遠不會關閉表單標記,因此表單最終會與重疊的ID值嵌套。

foreach($WOaccountsInfo as $WOInfo){ 
     $WOT .="<tr>"; 
     $WOT .="<td>$WOInfo[1]</td>"; 
     $WOT .="<td>$WOInfo[2]</td>"; 
     $WOT .="<td>$WOInfo[3]</td>"; 
     $WOT .="<td>$WOInfo[4]</td>"; 
     $WOT .="<td><form method='post' action='/radiosite/other/index.php'>"; 
     $WOT .="<input type='hidden' value='$WOInfo[0]' name='ID' id='ID'>"; 
     $WOT .="<button type='submit' title='Edit'>Edit</button>"; 
     // The following line is changed. 
     $WOT .= "<button type='submit' title='Delete' name='action' 
value='delWOEntry' onclick='confDel()'>Delete</button> 
</form></td>"; // <-- Add </form> here 
     $WOT .="</tr>"; 
     } 

我添加了一些換行符,以便更容易看到更改。

+0

我從健身房開車回辦公室時,當我看到通知「我沒有辦法不關閉窗體......」,但我沒有,並關閉它保存了代碼!謝謝你,先生! –

+1

不客氣! – RichGoldMD

相關問題