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

时间:2012-07-04 07:18:46

标签: php mysql

我有一个简单的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行“''','')附近使用正确的语法

所以我认为它必须与'author'列有关,所以我也删除了它,留下了这个查询:

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')时,它仍然会失败,这对我来说是完全无法理解的。

2 个答案:

答案 0 :(得分:0)

尝试转义这些

$small_image="some_image_url";
$large_image="some_image_url";
在包含在您的查询之前

答案 1 :(得分:-1)

你的问题在这里:

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

围绕作为数组的变量,您需要使用花括号{}

例如:

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

这是因为 PHP 会理解您输入的内容。所以当你回应它时,它会像你期望的那样回显出数组的内容。但 SQL 不会。