从sql DB php中删除行

时间:2018-02-12 22:24:46

标签: php database sqlite pdo

我的代码有问题,因为我无法从数据库中删除记录。

一切似乎都没问题,它会显示有关删除的信息,但它会保留在数据库中。这是代码:

<?php

$pdo = new PDO('sqlite:Elections.db');

if(isset($_POST['submit'])) {

    if ( isset($_POST['confirm']) && $_POST['confirm'] == 'yes'){
        try{


            $sql= "DELETE FROM Elections WHERE id=:ELectionId";

            $stmt = $pdo->prepare($sql);

            $ElectionId = filter_input(INPUT_POST, 'ElectionId', FILTER_SANITIZE_NUMBER_INT);
            $stmt->bindValue(':ElectionId', $ElectionId, PDO::PARAM_INT);

            $stmt->execute();
            $ppdo = null;

            $ElectionId = filter_input(INPUT_POST, 'ElectionId', FILTER_SANITIZE_NUMBER_INT);
            $stmt->bindValue(':ElectionId', $ElectionId, PDO::PARAM_INT);

            $stmt->execute();
            $ppdo = null;
            echo '<h1>Event removed</h1>';
            echo '<a href="AdminElections.php">Return to Admin menu</a>';

        } catch (PDOException $e ){
            print "We had an error: " . $e->getMessage(). "<br/>";
            die();
        }
    } else {
        echo 'You need confirm.';

    }

1 个答案:

答案 0 :(得分:-1)

你有一个错字:

.mat-row:focus
{
   outline: none;
}

应该是(“l”小写“):

$sql= "DELETE FROM Elections WHERE id=:ELectionId";

编辑:

来自@Mike的宝贵评论

这就是为什么你应该总是设置错误模式,如$sql= "DELETE FROM Elections WHERE id=:ElectionId"; (这是最明智的),或者在每次查询后手动检查$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);PDO::errorCode()方法。