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

时间:2016-02-02 14:36:03

标签: php mysql pdo delete-row

这是我的删除页面:

<?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”中生成的表格:

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

1 个答案:

答案 0 :(得分:1)

首先尝试:

<?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注入。该值在execute中传入,或者您可以绑定它(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

相关问题