向mysql插入数据时错误,Duplicate entry '0' for key 1 本帖最后由 pankay 于 2012-10-20 19:07:50 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 网上找些问答,听说是字段重复、我就删除了表里的所有数据、还是不行、、感觉问题出在插入数据的时候goods_id 没有赋值成功、 下面是出问题的代码,大家看看问题在哪里?<?phpdefine('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是假日价,et_prices是nomal_price $shop_price = (float)$data['WorkDayPrice']; $et_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, et_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', '$et_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";?> “本来字段good_id是自增类型,取消goods_id自增后才出现”,你的问题已经很清楚了,如果不用自增,就指定值,如果它是主键或是unique key是不能重复的。 主键一定要自增么,我插入的数据也是从别的数据库调用的,一定不会重复的。用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} 刚发现问题了,//团的编号id $good_id = $data['Id']; echo $good_id."\n\r";// 输出没有数据;奇怪,怎么赋值传不过去? $data['Id']就没取回来吧,你直接打印下$output看看内容 招聘asp高手 看看我用的adodb连接数据库添加数据怎么都提示错误呢? 请问解决方法 跪求 ! 友情链接保存到文件中,然后再从文件中读取显示 上一张下一张处理 Thinkphp中一个小问题 PHP phpmailer问题,,,谢谢大家帮助 XAMPP 错误 点击一个按钮,实现将多张图片打包下载,需要有提示那种,就是提示将打包文件放到什么目录的那种,求指导 php电脑版网站和wap版互相跳转的问题请教 新手~~ smarty的表单传值问题 一个网站,记录各模块点击量的问题。
下面是出问题的代码,大家看看问题在哪里?<?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是假日价,et_prices是nomal_price
$shop_price = (float)$data['WorkDayPrice'];
$et_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, et_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', '$et_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";?>
主键一定要自增么,我插入的数据也是从别的数据库调用的,一定不会重复的。用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}
//团的编号id
$good_id = $data['Id']; echo $good_id."\n\r";// 输出没有数据;奇怪,怎么赋值传不过去?