gpt4 book ai didi

php - 更新时 SQL 语法错误

转载 作者:行者123 更新时间:2023-11-29 21:04:17 26 4
gpt4 key购买 nike

我正在尝试为用户设置一种设置方式,我将设置以 json 格式保存在数据库中。当我尝试更新用户时,出现以下语法错误:

Warning: PDOStatement::execute(): 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 ''settings' = '{\"background-color\":\"050505\"}' WHERE ID = '2'' at line 1 in C:...\htdocs\app\model\model.database.php on line 29

这是我的代码。

public function setColor(){
$modUser = $this->model('user');
$modInput = $this->model('input');
$modViewData = $this->model('viewData');
$modUser->setSetting("background-color",str_replace('#', "", $modInput->returnPost("color")));
$this->view('profile/view.profile', $modViewData->getData());
}
//in User model
public function setSetting($name, $value){
$settings = $this->getSetting();
$settings[$name] = $value;
$settings = json_encode($settings);
$this->update("settings", $settings);
}
public function update($field, $value){
$sql = "UPDATE `users` SET :field = :value WHERE `ID` = :id";
$params = [":field" => $field, ":value" => $value, ":id" => $this->_data->ID];
$database = $this->model('database');
$database->query($sql,$params);
}

最佳答案

您无法参数化表名和列名。您需要将它们直接插入到查询字符串中。一种方法是:

$sql = "UPDATE `users` SET $field = :value WHERE `ID` = :id";

关于php - 更新时 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com