2

我有一个简单的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')它仍然失败,这对我来说是完全无法解释的。

4

2 回答 2

0

尝试逃避这些

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

在包含在您的查询中之前

于 2012-07-04T07:36:31.150 回答
-1

你的问题在这里:

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

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

例如:

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

这是因为PHP会理解您输入的内容。因此,当您回显此内容时,它将按您的预期回显数组的内容。但SQL不会。

于 2012-07-04T07:46:30.693 回答