删除数据库 table 中的特定行并生成 html table

Delete specific row in database table and generatet html table

这是我的删除页面:

<?php 
require('includes/config.php'); 

    $id = $_GET['ID'];
    $pdoConnect = new PDO($db);
    $query='DELETE * FROM studentraspored WHERE ID = "' . $id . '" ';
    $pdoResult = $db->prepare($query);

    $pdoExec = $pdoResult->execute($query);

    header('location:index.php');
?>

这是在我的“memberpage.php”中生成的table:

if (count($rows)){
    foreach ($rows as $row) {       
        $_SESSION['row'] = $rows;
        $id = floatval($row['ID']);
        echo "<tr>" .
            '<form action="delete_raspored.php" method="post">'.
            "<td>" . $row["ID"] . "</td>" .
            "<td>" . $row["den"] . "</td>" .
            "<td>" . $row["chas"] . "</td>" .
            "<td>" . $row["predmet"] . "</td>" .
            "<td>" . $row["profesor"] . "</td>" .               
            "<td>" . $row["prostorija"] . "</td>" .
            "<td>" . $row["tip"] . "</td>" .
            '<td><input type="submit" id="' . $id . '" value="Delete" ></td>'.
            "</form>".
            "</tr>"

这无法正常工作。我不明白为什么我错过了 floatval

先试试这个:

<?php 
require('includes/config.php'); 
$id = $_GET['ID'];
$query='DELETE FROM studentraspored WHERE ID = ?';
$pdoResult = $db->prepare($query);
$pdoResult->execute(array($id));
header('location:index.php');
exit();

注意占位符代替实际值,这将防止 SQL 注入。该值在执行中传入,或者您可以绑定它(http://php.net/manual/en/pdostatement.bindparam.php). http://php.net/manual/en/pdo.prepared-statements.php

delete 语法也被关闭,delete 删除整行而不是特定列,http://dev.mysql.com/doc/refman/5.7/en/delete.html

在您的 form 中,我也没有看到名为 ID 的元素,所以这可能是另一个问题,您的表单是通过 POST 而不是 GET 提交的.