奇怪的 MySQL 错误。 (PHP)

发布于 2024-12-12 04:00:00 字数 510 浏览 0 评论 0原文

我有以下代码:

<?php
include("config.php");
$key = 'blahblah';
$sql = "INSERT INTO softversions SET key='$key'";
$result = mysql_query($sql) or die ($mysql_error());
echo "dude";
?>

这给了我一个错误:

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 'key='svksskjfvns'' at line 1

问题是我已经在其他页面上使用了这个脚本大约一百次并且它有效。 表和字段名称 100% 正确。

我不明白发生了什么事。 您看到那里的语法错误了吗?

I have a following code:

<?php
include("config.php");
$key = 'blahblah';
$sql = "INSERT INTO softversions SET key='$key'";
$result = mysql_query($sql) or die ($mysql_error());
echo "dude";
?>

This gives me an error:

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 'key='svksskjfvns'' at line 1

The thing is that I've used this script about a hundred times on other pages and it worked.
Table and field names are 100% correct.

I don't understand what is going on.
Do you see the syntax error there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平安喜乐 2024-12-19 04:00:01

key是MySQL中的保留字。要将其用作列,每次调用时都需要对其进行转义。

$sql = "INSERT INTO softversions SET `key`='$key'";

key is a reserved word in MySQL. To use it as a column, you need to escape it every time you call it.

$sql = "INSERT INTO softversions SET `key`='$key'";
多彩岁月 2024-12-19 04:00:01

$sql = "插入 softversions(keyName) 值('{$key}')";

$sql = "INSERT INTO softversions(keyName) values('{$key}')";

鹿童谣 2024-12-19 04:00:00

KEY 是一个保留字 在 MySQL 中,您需要使用反引号将其转义以将其用作列名,并且在插入时也不应该使用 SET

$sql = "INSERT INTO softversions (`key`) VALUES ('$key')";

KEY is a reserved word in MySQL and you need to escape it using backticks to use it as a column name and also you should not use SET when inserting.

$sql = "INSERT INTO softversions (`key`) VALUES ('$key')";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文