PHP PDO占位符不会删除

时间:2015-05-07 21:50:40

标签: php mysql pdo

我试图删除表格' telefoonnummers'中的记录。 但没有变化。下面的错误是什么?

<HTML>
  <?php
        require_once 'db_config.php';

        //get data last page
            $indexnr = $_GET['id'];

            $sth = $dbh->prepare("DELETE FROM telefoonnummers WHERE index = :indexnr");
            $sth->bindValue(':indexnr', $indexnr, PDO::PARAM_INT);
            $count = $sth->execute();

            if($count == 1) {
        print "One record deleted!<br>";
            }
            else 
            {
        print "None record deleted!";
            }
        //Back to first page
?>
        <a href="admin.php">Back</a>
</HTML>

2 个答案:

答案 0 :(得分:1)

这里似乎有一些基本的调试 - 检查查询是否有效。我没有看到你的代码有任何明显错误。

try {
    $count = $sth->execute();
} catch (PDOException $e) {
    // do error handling
    var_dump($e); // use only for debug
}

echo $_GET['id'];验证您是否从GET中提取了正确的信息。

echo $count;以确认您正在进行适当的比较。

无论哪种方式,最好将关键的PDO查询包含在try / catch中。

答案 1 :(得分:0)

object(PDOException)#3 (8) { ["message":protected]=> string(213) "SQLSTATE[42000]: 
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index = '4'' at line 1" 

["string":"Exception":private]=> string(0) "" ["code":protected]=> string(5) "42000" ["file":protected]=> string(50) "C:\USBwebserver\root\PhpProjectWeek5\verwijder.php" ["line":protected]=> int(14) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(50) 
"C:\USBwebserver\root\PhpProjectWeek5\verwijder.php" ["line"]=> int(14) ["function"]=> string(7) "execute" ["class"]=> string(12) "PDOStatement" ["type"]=> string(2) "->" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> array(3) { [0]=> string(5) "42000" [1]=> int(1064) [2]=> string(157) "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index = '4'' at line 1" } } 
  

注意:未定义的变量:在第21行的C:\ USBwebserver \ root \ PhpProjectWeek5 \ verwijder.php中计数       没有记录被删除!回到

相关问题