一个查询中有不同的 MySQL 语法错误


Different MySQL syntax errors in one query

我有一个简单的INSERT INTO查询,每次都失败。奇怪的是,如果我取出错误最初指向的列或之前的列,我只会得到不同的错误。

这是查询:

mysql_query("insert into list_items
 (list_id,position,item,small_image,large_image,asin,description,
  author,publish_date,amazon_description) values  
 ('$id','$key','$value','$small_image','$large_image','$asin',
  '$descriptions[$key]','$authors[$key]','$publish_date','$amazon_description')")
or die(mysql_error());

我使用的示例数据是:

$key=1;
$value=mysql_real_escape_string("Harry Potter and the Sorcerer's Stone");
$small_image="some_image_url";
$large_image="some_image_url";
$asin="13412341234";
$descriptions[$key]="";
$authors[$key]="JK Rowling";
$publish_date="1999-09-08";
$amazon_description=mysql_real_escape_string($long_amazon_description);

我得到的错误是:

您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的"1999-09-08")附近使用的正确语法

当我从查询中删除"publish_date"列时,我收到一个不同的错误,说:

您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的""、"附近使用的正确语法

所以我认为它一定与"作者"列有关,所以我也删除了它,留下了这个查询:

mysql_query("insert into list_items 
 (list_id,position,item,small_image,large_image,
  asin,description,amazon_description) values 
 ('$id','$key','$value','$small_image','$large_image',
 '$asin','$descriptions[$key]','$amazon_description') ")
or die(mysql_error());

。但我得到了同样的错误。有人可以告诉我我在这里做错了什么吗?这是表结构:

CREATE TABLE IF NOT EXISTS `list_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `list_id` int(11) NOT NULL,
  `position` int(11) NOT NULL,
  `item` varchar(512) NOT NULL,
  `item_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `voted_up` int(11) NOT NULL,
  `voted_down` int(11) NOT NULL,
  `small_image` varchar(128) NOT NULL,
  `large_image` varchar(128) NOT NULL,
  `asin` varchar(16) NOT NULL,
  `description` mediumtext NOT NULL,
  `author` varchar(128) NOT NULL,
  `publish_date` varchar(16) NOT NULL,
  `genre` varchar(128) NOT NULL,
  `amazon_description` mediumtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=379 ;

查询打印为:

insert into list_items
(list_id,position,item,small_image,large_image,asin,description,
author,publish_date,amazon_description) values
('76','1','Harry Potter And The Sorcerer''s Stone',
 'http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL160_.jpg',
 'http://ecx.images-amazon.com/images/I/51MU5VilKpL.jpg','059035342X','',
 'J.K. Rowling','1999-09-08',
 'Harry Potter has no idea how famous he is. That''s because he''s being raised by his miserable aunt and uncle who are terrified Harry will learn that he''s really a wizard, just as his parents were. But everything changes when Harry is summoned to attend an infamous school for wizards, and he begins to discover some clues about his illustrious birthright. From the surprising way he is greeted by a lovable giant, to the unique curriculum and colorful faculty at his unusual school, Harry finds himself drawn deep inside a mystical world he never knew existed and closer to his own noble destiny.')

重要编辑:

当查询减少到insert into list_items (list_id,position) values ('82','1')时它仍然失败,这对我来说是完全无法解释的。

尝试转义这些

$small_image="some_image_url";
$large_image="some_image_url";

在包含在查询中之前

你的问题在这里:

... values ('$id','$key','$value','$small_image','$large_image',
 '$asin','$descriptions[$key]','$amazon_description') ")

在数组变量周围,您需要{}大括号。

例如:

 '{$descriptions[$key]}','{$authors[$key]}'

这是因为 PHP 会理解你在键入什么。 因此,当您回显此内容时,它将如您所期望的那样回显数组的内容。但SQL不会。