0

我有这个 PHP 代码:

$ua = $mysqli->real_escape_string($_SERVER['HTTP_USER_AGENT']);

$mysqli->query("INSERT INTO browsers(useragent, account, allowed, key, ip) VALUES('$ua', {$userR['id']}, 0, '(Key/token)', '(IP address)')");

这是以下 SQL 查询:

INSERT INTO browsers(useragent, account, allowed, key, ip) VALUES('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17', 8, 0, 'bc132b38f35a01ce', '127.0.0.1')

但是:即使在转义查询后也不起作用:

#1064 - 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, ip) VALUES('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like' at line 1
4

1 回答 1

2

RESERVED KEYWORD您在查询中使用了 mysql ,即key只需用反引号 ` 将其括起来让数据库了解它是一列

$mysqli->query("INSERT INTO browsers(useragent, account, allowed, `key`, ip) VALUES('$ua', {$userR['id']}, 0, '(Key/token)', '(IP address)')");
于 2013-05-28T08:06:32.690 回答