【问题标题】:I keep getting an error when i try to use UPDATE to update my database当我尝试使用 UPDATE 更新我的数据库时,我不断收到错误消息
【发布时间】:2022-11-28 17:13:50
【问题描述】:

每当有人赢得比赛时,我都试图将分数加 1,但我一直收到此错误。 我不太确定我做错了什么。

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ID = 61a379cd4798f' at line 1 in C:\xampp\htdocs\dicegame\update.php:16 Stack trace: #0 C:\xampp\htdocs\dicegame\update.php(16): PDOStatement->execute() #1 C:\xampp\htdocs\dicegame\index.php(161): Update->scoreUpdate(Object(PDO)) #2 {main} thrown in C:\xampp\htdocs\dicegame\update.php on line 16
<?php 
    
class Update{

    private $score;
    private $id;

    public function scoreUpdate($conn){

        $this->id = $_SESSION['id'];

        $sql = "update user set score = score + 1 ID = $this->id";

        $stmt = $conn->prepare($sql);

        $stmt->execute();

    }
}


?>

【问题讨论】:

  • score + 1 ID...根据您的意图,您需要在ID 之前添加WHERE,

标签: php mysql pdo


【解决方案1】:

它应该是:

$sql = "update user set score = score + 1 where ID = $this->id";

我也对 sql 注入持开放态度。

【讨论】:

    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2019-07-19
    • 2020-07-26
    • 1970-01-01
    • 2016-05-05
    相关资源
    最近更新 更多