简体   繁体   中英

PHP PDO placeholders will not DELETE

I have tried to delete a record in table 'telefoonnummers'. But nothing changes. What is the error below?

<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>

Some basic debugging seems to be in order here -- check to see if the query worked. I don't see anything obviously wrong with your code.

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

echo $_GET['id']; to verify that you're pulling the the right information from GET .

echo $count; to verify that you're making a proper comparison.

Either way, it's good practice to have your critical PDO queries wrapped in a try/catch.

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" } } 

Notice: Undefined variable: count in C:\\USBwebserver\\root\\PhpProjectWeek5\\verwijder.php on line 21 None record deleted! Back

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM