简体   繁体   English

删除不适用于pdo

[英]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). (我正在使用我的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 我必须使用外键更改约束: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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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