SQL 替换或插入语法错误 [英] SQL Replace or Insert Into Syntax error
问题描述
我无法弄清楚出了什么问题.我的查询如下所示:
I cannot figure out what is wrong. My query looks like this:
INSERT OR REPLACE INTO lang (name, string, lang) VALUES ('PAGE_TITLE', 'Page Title', 'en')
这是我执行查询的地方:
And this is where I execute that query:
foreach($lang_fields as $field){
$update = "INSERT OR REPLACE INTO lang (name, string, lang)
VALUES ('".mysqli_real_escape_string($conn, $field)."',
'".mysqli_real_escape_string($conn, $_POST[$field])."',
'".mysqli_real_escape_string($conn, $_POST['lang_load'])."')";
echo $update;
$sql = mysqli_query($conn, $update)
or die(mysqli_error($conn));
}
这是错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'OR REPLACE INTO lang (name, string, lang) VALUES ('PAGE_TITLE', 'P' at line 1
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR REPLACE INTO lang (name, string, lang) VALUES ('PAGE_TITLE', 'P' at line 1
推荐答案
MySQL 不支持我见过的任何 INSERT OR REPLACE INTO
语法.您可能正在寻找类似 INSERT INTO ... ON DUPLICATE KEY UPDATE
的东西.在您的示例中,它可能如下所示:
MySQL does not support any INSERT OR REPLACE INTO
syntax that I've seen. You may be looking for something like INSERT INTO ... ON DUPLICATE KEY UPDATE
. In your example it may look like this:
INSERT INTO myTable (name, string, lang) VALUES (param1, param2, param3)
ON DUPLICATE KEY UPDATE name = param1, string = param2, lang = param3;
可以在此处的文档中找到更多信息:https://dev.mysql.com/doc/refman/5.0/en/insert.html
More information can be found in the documentation here: https://dev.mysql.com/doc/refman/5.0/en/insert.html
可以使用此 SQL Fiddle 找到一个示例.
An example of this can be found using this SQL Fiddle.
这篇关于SQL 替换或插入语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!