向mysql插入数据时错误,Duplicate entry '0' for key 1

麦兜的春天 2012-10-20 06:54:19
向mysql插入数据时错误,Duplicate entry '0' for key 1 ,本来字段good_id是自增类型,取消goods_id自增后才出现的


MySQL server error report:Array ( [0] => Array ( [message] => MySQL Query Error ) [1] => Array ( [sql] => INSERT INTO `tour`.`gd_goods`(goods_id, cat_id, goods_sn, goods_name, goods_name_style, click_count, brand_id, provider_name, goods_number, goods_weight, market_price, shop_price, promote_price, promote_start_date, promote_end_date, warn_number, keywords, goods_brief, goods_desc, goods_thumb, goods_img, original_img, is_real, extension_code, is_on_sale, is_alone_sale, is_shipping, integral, add_time, sort_order, is_delete, is_best, is_new, is_hot, is_promote, bonus_type_id, last_update, goods_type, seller_note, give_integral, rank_integral, suppliers_id, is_check)VALUES ('', '0', '', '深圳最新COCO PARK大型購物中心一天團', '', '0', '', '0', '1', '', '0', '79', '89', '', '2012/10/21 0:00:00', '', '', '', '', '', '', '', '1', '2905', '0', '0', '0', '', '', '', '', '0', '0', '0', '0', '', '', '0', '0', '', '', '', '') ) [2] => Array ( [error] => Duplicate entry '0' for key 1 ) [3] => Array ( [errno] => 1062 ) )
...全文
1418 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
StAlan 2012-10-22
  • 打赏
  • 举报
回复
$data['Id']就没取回来吧,你直接打印下$output看看内容
麦兜的春天 2012-10-20
  • 打赏
  • 举报
回复
刚发现问题了,


//团的编号id
$good_id = $data['Id'];

echo $good_id."\n\r";// 输出没有数据;奇怪,怎么赋值传不过去?
麦兜的春天 2012-10-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
“本来字段good_id是自增类型,取消goods_id自增后才出现”,你的问题已经很清楚了,如果不用自增,就指定值,如果它是主键或是unique key是不能重复的。
[/Quote]

主键一定要自增么,我插入的数据也是从别的数据库调用的,一定不会重复的。

用phpmyadmin看数据库,发现goods_id没有插入、
原始数据是这样的:
{"Id":113023,"TourId":2905,"ProductCode":"SZX01","ProductName":"深圳最新COCO PARK大型購物中心一天團","ProcessDays":1,"ProcessResume":"","WorkDayPrice":79.0000,"WeekdayPrice":89.0000,"NormalPrice":0.0000,"Catalog":"0","Type":0,"RDCatalog":0,"BizCatalog":0,"StartDate":"2012-10-18","Deleted":0}

craboy1 2012-10-20
  • 打赏
  • 举报
回复
“本来字段good_id是自增类型,取消goods_id自增后才出现”,你的问题已经很清楚了,如果不用自增,就指定值,如果它是主键或是unique key是不能重复的。
麦兜的春天 2012-10-20
  • 打赏
  • 举报
回复

下面是出问题的代码,大家看看问题在哪里?

<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
header ("Content-Type:text/html; charset=UTF-8");

$url = 'http://58.61.153.173/dataport/GetAllTourInfo.ashx';

$host=array('user: gdtourserviceuser','password: gdtour!123.@983_???000');

$ch = curl_init();
set_time_limit(0);
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$host);

$output = curl_exec($ch);
if ($output === FALSE) {

echo "cURL Error: " . curl_error($ch);

}

curl_close($ch);
$obj=json_decode($output,TRUE);
//取到站外传来的数据,循环插入本地数据库
foreach($obj['data'] as $data)
{
//团的编号id
$good_id = $data['Id'];
//约定extension_code是团de线id
$extension_code= $data['TourId'];
//线路id
$cat_id = (int)$data['Catalog'];
//线路编号
$good_sn = (string)$data['ProductCode'];
//线路名称
$goods_name = $data['ProductName'];
//行程天数
$goods_number= $data['ProcessDays'];
//行程简介
$goods_bref = $data['ProcessResume'];
//约定 shop_price 是平日价,promote_price是假日价,market_prices是nomal_price
$shop_price = (float)$data['WorkDayPrice'];
$market_price = (float)$data['NormalPrice'];
$promote_price =(float)$data['WeekdayPrice'];

//是否是自营线路,代理线路
$provider_name= $data['BizCatalog'];
//散团,包团
$goods_type = $data['Type'];
//0-系列线,1-另写线
$seller_note = $data['RDCatalog'];
//出发时间



$goods_brief = '';
$promote_end_date =(string)$data['StartDate'];
$click_count = 0;
$goods_name_style ='';
$brand_id = '';
$is_promote = 0;
$promote_start_date = '';
$goods_img = '';
$goods_thumb = '';
$original_img = '';
$keywords = '';
$warn_number = '';
$integral = '';
$goods_weight = '';
$give_integral = '';
$is_best = 0;
$is_new = 0;
$is_hot = 0;
$is_on_sale = 0;
$is_alone_sale = 0;
$is_shipping = 0;
$goods_desc = '';
$rank_integral = '';
$suppliers_id ='';
$is_real = 1;
$is_check = '';
$num = 1;

$sql = "INSERT INTO ". $GLOBALS['ecs']->table('goods')."(goods_id, cat_id, goods_sn, goods_name, goods_name_style, click_count, brand_id, provider_name, goods_number, goods_weight, market_price, shop_price, promote_price, promote_start_date, promote_end_date, warn_number, keywords, goods_brief, goods_desc, goods_thumb, goods_img, original_img, is_real, extension_code, is_on_sale, is_alone_sale, is_shipping, integral, add_time, sort_order, is_delete, is_best, is_new, is_hot, is_promote, bonus_type_id, last_update, goods_type, seller_note, give_integral, rank_integral, suppliers_id, is_check)". "VALUES
('$goods_id', '$cat_id', '$goods_sn', '$goods_name', '$goods_name_style', '$click_count', '$brand_id', '$provider_name', '$goods_number', '$goods_weight', '$market_price', '$shop_price', '$promote_price', '$promote_start_date', '$promote_end_date', '$warn_number', '$keywords', '$goods_brief', '$goods_desc', '$goods_thumb', '$goods_img', '$original_img', '$is_real', '$extension_code', '$is_on_sale', '$is_alone_sale', '$is_shipping', '$integral', '$add_time', '$sort_order', '$is_delete', '$is_best', '$is_new', '$is_hot', '$is_promote', '$bonus_type_id', '$last_update', '$goods_type', '$seller_note', '$give_integral', '$rank_integral', '$suppliers_id', '$is_check')";


$GLOBALS['db']->query($sql);


}


echo "insert sucess!\n\r";



?>


麦兜的春天 2012-10-20
  • 打赏
  • 举报
回复
网上找些问答,听说是字段重复、我就删除了表里的所有数据、还是不行、、感觉问题出在插入数据的时候goods_id 没有赋值成功、

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧