输入字段提交最后一个索引,而不是选择索引

时间:2017-07-17 23:39:08

标签: php mysql sql

我从数据库中动态填充表格(为简洁起见,所有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 个答案:

答案 0 :(得分: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>";
        }

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