简体   繁体   中英

delete not working with pdo

The following code is not working even though I've tested it by putting this on the page alone. To make this clear just after this query I'm doing a select query to display all the user in the DB.

$db->query("DELETE FROM Projet_Client WHERE username = '$_GET[d]'");
echo "<div class='alert alert-success text-middle'><strong>Succès</strong>, le 
client a été supprimé.</div>";

Here is the "echo" of the query to show you how it looks like :

DELETE FROM Projet_Client 
WHERE 
username = 'a75ea99ce47306ec259d4c905bb9c3f762a531ee'

(I'm using my sql). Thank you.

I changed the code ant it looks like this :

    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
    try {
        $stmt = $db->prepare('DELETE FROM Projet_Client WHERE username= :username');
        $stmt->bindParam(':username', $_GET['d']); 
        $stmt->execute();

    } catch(Exception $e){
            echo 'Exception -> ';
            var_dump($e->getMessage());
    }

However no exception are being throw.

The problem has been solved. I had to change the constraint with the foreign key : ON DELETE = CASCADE

Are you sure that the query is being executed properly ? Your query looks okay to me.

Maybe you can try to echo the error (if there is one) by using this :

if ($conn->query($sql) !== false) {
echo "Record deleted successfully";} else {
echo "Error deleting record: " . $conn->error;

}

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