重复更新的PHP PDO MYSQL

时间:2017-04-25 09:38:57

标签: php mysql pdo

我尝试使用重复密钥更新进行MySQL查询但我无法看到语法错误,因为得到"语法错误或访问冲突"错误。

代码:

$sql = "INSERT INTO site_configuration 
          (configuration_key, configuration_value, additional_data) 
        VALUES 
          (:config_key, :site_new_background, :site_background_meta)
        ON DUPLICATE KEY UPDATE
          'configuration_value' = VALUES(:site_new_background), 'additional_data' = VALUES(:site_background_meta)";
$smth = $database->prepare($sql);
$smth->bindParam(':config_key', $config_key, PDO::PARAM_STR);
$smth->bindParam(':site_new_background', $site_new_background, PDO::PARAM_STR);
$smth->bindParam(':site_background_meta', $site_background_meta, PDO::PARAM_STR);

错误讯息:

' PDOException' with message' SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在'" configuration_value'附近使用。 = VALUES(?),' additional_data' = VALUES(?)'

1 个答案:

答案 0 :(得分:1)

从语句中删除单引号

$sql = "INSERT INTO site_configuration 
              (configuration_key, configuration_value, additional_data) 
            VALUES 
              (:config_key, :site_new_background, :site_background_meta)
            ON DUPLICATE KEY UPDATE
              configuration_value = VALUES(:site_new_background), additional_data = VALUES(:site_background_meta)";