duanli6834 2016-02-02 14:36
浏览 32
已采纳

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

This is my delete page :

<?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');
?>

This is generated table in my “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>"

This not working properly. I don't understand why maybe something i missed with floatval

  • 写回答

1条回答 默认 最新

  • douzhi3776 2016-02-02 14:50
    关注

    Start by trying this:

    <?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();
    

    Note the placeholder in place of the actual value, this will prevent SQL injections. The value is passed in in the execute, or you could bind it (http://php.net/manual/en/pdostatement.bindparam.php). http://php.net/manual/en/pdo.prepared-statements.php

    The delete syntax was also off, delete deletes a whole row not specific columns, http://dev.mysql.com/doc/refman/5.7/en/delete.html.

    In your form I also don't see an element named ID so that could be another issue and your form is submitting via POST, not GET.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离