我有以下sql查询,我想用PHP运行。 为什么不起作用?
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服务器版本对应的手册,以便在'IF EXISTS(SELECT * FROM content WHERE site_id ='1')附近使用正确的语法。在第1行开始更新内容SET tit'
答案 0 :(得分:2)
我认为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])) ."';