【问题标题】:SQL Query IF EXISTS not workingSQL 查询 IF EXISTS 不起作用
【发布时间】:2015-12-09 11:10:21
【问题描述】:

我有以下要使用 PHP 运行的 sql 查询。 为什么它不起作用?

IF EXISTS(SELECT * FROM content WHERE site_id ='". $id ."') BEGIN
UPDATE content SET
titel = '". htmlentities($connect->real_escape_string($data[0])) ."',
content = '". htmlentities($connect->real_escape_string($data[1])) ."'
WHERE site_id ='". $id ."'
END
ELSE
BEGIN
INSERT INTO content (site_id, titel, content)
VALUES (
'". $id ."',
'". htmlentities($connect->real_escape_string($data[0])) ."',
'". htmlentities($connect->real_escape_string($data[1])) ."'
)
END

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'IF EXISTS(SELECT * FROM content WHERE site_id ='1') BEGIN UPDATE content SET tit' 附近使用正确的语法

【问题讨论】:

标签: php mysql


【解决方案1】:

我认为 site_id 是您表中的唯一键。使用@Saty提出的解决方案

INSERT INTO content (site_id, titel, content)
VALUES (
  '". $id ."',
  '". htmlentities($connect->real_escape_string($data[0])) ."',
  '". htmlentities($connect->real_escape_string($data[1])) ."'
)
ON DUPLICATE UPDATE 
  titel = '". htmlentities($connect->real_escape_string($data[0])) ."',
  content = '". htmlentities($connect->real_escape_string($data[1])) ."';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    相关资源
    最近更新 更多