dongzhanyan3667 2015-12-18 13:45
浏览 8

试图删除表中的条目。 查询不删除行,不知道如何调试

I'm trying to delete an entry in my database using the code below. The javascript function takes me to index.php?delpost= with the correct "adventureID" but when I check my database the row is still there. I've very recently started using PDO so I'm wondering if the execute() statement might be the issue. $dbh connect to my database at the top of the page and it is working as it prints every row from the table I'm trying to delete rows from. My goal is to successfully delete a row when I call the javascript function. The issue is - it doesn't.

<script language="JavaScript" type="text/javascript">
    function delpost(adventureID, title)
    {
        if (confirm("Are you sure you want to delete '" + title + "'" + " '" + adventureID + "'"))
        {
            window.location.href = 'index.php?delpost=' + adventureID;
        }
    }
</script>


<?php
    if(isset($_GET['delpost'])){
        $stmt = $dbh->prepare("DELETE FROM adventure WHERE adventureID = :adventureID");
        $stmt->execute(array(':adventureID' => $_GET['delpost']));
        header('Location: index.php?action=deleted');
        exit;
    }
?>


<?php
if(isset($_GET['action'])){
    echo '<h3>Post '.$_GET['action'].'.</h3>';
}

try {
    foreach($dbh->query("SELECT adventureID, title, postDate FROM adventure ORDER BY adventureID DESC") as $row) {
        echo '<tr>';
        echo '<td>'.$row['title'].'</td>';
        echo '<td>'.date('jS M Y', strtotime($row['postDate'])).'</td>';
        ?>

        <td>
            <a href="javascript:delpost('<?php echo $row['adventureID'] ?>','<?php echo $row['title'] ?>')">Delete</a>
        </td>

        <?php
        echo '</tr>';

    }

} catch(PDOException $e) {
    echo $e->getMessage();
}
?>
  • 写回答

2条回答 默认 最新

  • drl92080 2015-12-18 13:54
    关注

    Probably you are facing problem with MYSQL SAFE UPDATES being ON. To avoid it and be able to finally delete rows, you can engage in the following tactics:

    SET SQL_SAFE_UPDATES = 0;
    
    --- YOUR DELETE STATEMENT ---
    
    SET SQL_SAFE_UPDATES = 1;
    

    To check if you have SQL_SAFE_UPDATES enabled you can do by running:

    SHOW VARIABLES LIKE 'sql_safe_updates'
    
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效