<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('test');
mysql_query("set names 'utf8'");
 function insert($table, $data)
{
if(is_array($data)){
$bind = array();
$vals = '';
$keys = '';
foreach($data as $key=>$value){
$keys.= $keys ? ',`'.trim($key).'`' : '`'.trim($key).'`';
$vals.= $vals ? ',\''.$value.'\'' : '\''.$value.'\'';
$bind[] = $value;
}
$sql = 'INSERT INTO ' . $table . '(' . $keys . ') VALUES(' . $vals . ')';
echo $sql, '<br />';
mysql_query($sql) or die(mysql_error());
}
else
return false;
}
function fopen_utf8 ($filename)
    {
        $encoding = '';
        $handle = fopen($filename, 'r');
        $bom = fread($handle, 2);
        //    fclose($handle);
        rewind($handle);
        if ($bom === chr(0xff) . chr(0xfe) || $bom === chr(0xfe) . chr(0xff)) {
            // UTF16 Byte Order Mark present
            $encoding = 'UTF-16';
        } else {
            $file_sample = fread($handle, 1000) + 'e'; //read first 1000 bytes
            // + e is a workaround for mb_string bug
            rewind($handle);
            $encoding = mb_detect_encoding($file_sample, 
            'UTF-8, UTF-7, ASCII, EUC-JP,SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP');
        }
        if ($encoding) {
            stream_filter_append($handle, 
            'convert.iconv.' . $encoding . '/UTF-8');
        }
        return ($handle);
    }$handle = fopen_utf8("1.csv","r");  // 'goods_sn' => $goods->getDefaultGoodsSn(),  //编码
function getDefaultGoodsSn(){
$sql="select max(id) as maxId from eb_goods";
//echo $sql, '<br />';
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
$tmpArray[] = $row;
}
$maxId=$tmpArray[0]["maxId"];
$a = $maxId+1;
//echo $a, '<br />';
//print_r($tmpArray);
return "M".str_pad($maxId+1,6,"0",STR_PAD_LEFT);
}

while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) {
         //print_r($cols);
         //count($cols);
//echo $supply_id.'   '.$bid.'<br>';
         //包含闪电发货
         $sales = 1;
                //print_r($cols);
                
             $the_price = 9.00;
            
                $goods_info = array(
                'goods_name' => str_replace("'"," ",$cols[0]),  //商品名称
                'number' => $cols[9],  //库存
'class_id' => 1,   //一级分类
                'sort_id' => 1,  //二级分类
'genre_id' => 1,   //三级分类
                'cid' => $cols[1],  //类目
'brand_id' => 1,  //品牌id
                'goods_sn' => getDefaultGoodsSn(),  //编码
                //'price' => $cols[7],   //价格
                'price' => 9.00,   //价格
                'jinjia' => 9.00,   //进价
                'sc_price' => 9.00,  //市场价
                'addtime' => time(),  //添加时间
'is_taobao' => 1,   //是否淘宝数据包导入
                'isvalid' => 1,  //是否可用
                'taobao_id'=>$cols[44],
'smallpic' => '',   //图片,稍后插入
                'supply_id' => 1,//供应商
                'sales' => $sales
                );

                $taobaoid=str_replace("'"," ",$cols[0]);//商品名称
                $num=$cols[9];
                
                 //插入goods_info,返回id备用
                 $goodsid=insert("eb_goods", $goods_info);}?>
goods_sn 会重复请问是为什么 要求是唯一的 为什么会出现重复的
//获取系统默认货号
public function getDefaultGoodsSn(){
$sql="select max(id) as maxId from eb_goods";
$tmpArray=$this->_db->query($sql);
$maxId=$tmpArray[0]["maxId"];
return "M".str_pad($maxId+1,6,"0",STR_PAD_LEFT);
}

解决方案 »

  1.   

    函数 getDefaultGoodsSn 从 eb_goods 表的 id 字段构造出货号
    但待插入的 $goods_info 中并没有与 id 相关的数据
    如果 id 不是自增,则必然产生碰撞
      

  2.   

    既然 id 是主键,那就不会重复
    于是 $sql="select max(id) as maxId from eb_goods"; 后,maxId也不会重复
    这是一般的认为,在单用户情况下测试也不会有问题
    但是在多用户情况下就难说了:
    insert into ....
      如果此刻有别的用户也做了 insert into .... 使 id 加 1
    select max(id) ...
    这样取得的 max(id) 就不是你自己插入的记录的 id 了,这就产生重复的原因你应该用 mysql_insert_id 取回自己插入的记录的 id 来产生货号