借助UPDATE功能

时间:2019-05-04 08:33:40

标签: php sql pdo

我有一个在有人单击“删除帐户”按钮后运行的代码,它应将“已删除”值从0更改为1,但显然我的代码无法正常工作。有人可以帮我发现问题吗?

        $dsn = 'mysql:host=127.0.0.1;dbname=user_db;charset=utf8';

        $conn = new PDO($dsn, $username1, $password);
        $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "UPDATE users SET deleted = :1 WHERE username = :$username";
        $stmt= $conn->prepare($sql);
        $stmt->execute("1, $username");
        header("location:logout.php");

1 个答案:

答案 0 :(得分:0)

  

bindParam-将参数绑定到指定的变量名称

您需要使用绑定参数

 $stmt= $conn->prepare("UPDATE users SET deleted = '1' WHERE username = :username");
 $stmt->bindParam(':username', $username,PDO::PARAM_STR);
 $stmt->execute();
 header("location:logout.php");