2017-07-17 93 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

相关问题