<?php
session_start(1);
  ///upload.php
set_time_limit(60*60*60);
if (isset($_FILES['userfile'])){//include ("../../common/config.php");
$host="localhost";
$usr="root";
$pwd="";
$db="test_db";
$link = mysql_connect($host,$usr,$pwd) or die("Could not connect:". mysql_error());
mysql_select_db($db) or die("Could not connect:". mysql_error());
  ////deal with the upload file ;

//global $userfile;

if(!is_dir('../../shopping/merchant/temp/'))mkdir('../../shopping/merchant/temp');

$uploaddir = '../../shopping/merchant/temp/';

print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
$uploadfile=$uploaddir . $_FILES['userfile']['name'];
    print "File is valid, and was successfully uploaded.  Here's some more debugging info:\n";
    print_r($_FILES);

else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
}
//get data from upload file;
if(!$handle = fopen($uploadfile, 'r'))
{
echo("Can not open file!");
//exit();
}
//create a temp table
$temp="CREATE TABLE temp(pid VARCHAR( 30 ) NOT NULL ,
name VARCHAR( 200 ) NOT NULL ,
pic VARCHAR( 100 ) NOT NULL ,
list_price DECIMAL( 10, 2 ) NOT NULL ,
lour_price DECIMAL( 10, 2 ) NOT NULL ,
description VARCHAR(255) NOT NULL ,
enter_id INT NOT NULL DEFAULT '0',
cid VARCHAR( 30 ) NOT NULL,
t_name VARCHAR(100) NOT NULL,
status BOOL NOT NULL DEFAULT '1')";
if(!mysql_query($temp)){
sprintf("intenal error %d:%s\n",mysql_errno(),mysql_error());
//exit();
}
上传文件,创建临时表的部分

解决方案 »

  1.   

    while(!feof($handle)){
    $row=array();
    $lines=fgets($handle);
    $row=explode('","',$lines);
    foreach($row as $k=>$l)
    {
    if($k!=0)
    {
    switch($k)
    {
    case 1:$pid=$l;
    case 2:$name=$l;
    case 3:$pic=$l;
    case 4:$path=$l;
    case 5:$list_price=$l;
    case 6:$lour_prc=$l;
    case 7:$des=conts($l,' ','"');
    default:continue;
    }
    }
    }
    //create a temp table
    //insert into table cats
    $cat=explode("/",$path);
    foreach($cat as $j=> $line)
    {
    $table=$cat[0];
    $c_result=mysql_query("select name from cats where name='$line'") or die("2:".mysql_error());
    if(mysql_num_rows($c_result)==0)
    mysql_query("insert into cats(name,path) values('$line','$path')") or die("3:".mysql_error());
    }
    $c_query="select cid from cats where path='$path' and right(path,length(name))=name" ;
    $c_result=mysql_query($c_query)or die("2:".mysql_error());
    while($c_obj=mysql_fetch_object($c_result)){
    mysql_query("insert into temp(pid,name,pic,list_price,lour_price,description,cid,t_name) values ('$pid','$name','$pic','$list_price','$lour_prc','$des','".$c_obj->cid."','$cat[0]')") or die("3:".mysql_error());
    }
    //insert into table cats
    //echo $pid."<hr>";
    }
    ////mid未插入,商户表中查询插入
    ///insert into table product,if you find 
    $p_result1=mysql_query("select pid from books where status=1");
    while($p_obj1=mysql_fetch_object($p_result1))
    {
    $pid=$p_obj1->pid;
    //echo $pid;
    $t_result=mysql_query("update temp set 
    status=0 
    where pid ='$pid'") 
    or die("10:".mysql_error());

    }
    $t_result1=mysql_query("select 
    pid,
    name,
    pic,
    list_price,
    lour_price,
    description,
    cid,
    t_name from temp where status=1") 
    or die("6:".mysql_error());
    $j=mysql_num_rows($t_result1);
    $p_result=mysql_query("select id,status from books where status=0");
    $k=mysql_num_rows($p_result);
    if($k>=$j){
    for($i=0;$i<$j;$i++)
    {
    $t_obj=mysql_fetch_object($t_result1);
    $p_obj=mysql_fetch_object($p_result);
    $id=$p_obj->id;
    $strquery="update books set 
    pid='".$t_obj->pid."',
    name='".$t_obj->name."',
    pic='".$t_obj->pic."',
    list_price='".$t_obj->list_price."',
    lour_price='".$t_obj->lour_price."',
    description='".$t_obj->description."',
    status=1 where status=0 and id='$id'";
    mysql_query($strquery)
    or die("13$strquery:".mysql_error());

    }
    }
    else
    {
    //$num=$j-$k;
    for($ii=0;$ii<$k;$ii++)
    {
    $t_obj=mysql_fetch_object($t_result1);
    $p_obj=mysql_fetch_object($p_result);
    $id=$p_obj->id;
    $strquery="update books set 
    pid='".$t_obj->pid."',
    name='".$t_obj->name."',
    pic='".$t_obj->pic."',
    list_price='".$t_obj->list_price."',
    lour_price='".$t_obj->lour_price."',
    description='".$t_obj->description."',
    status=1 where status=0 and id='$id'";
    mysql_query($strquery)
    or die("13$strquery:".mysql_error());
    }
    $l_num=$k;
    $squery="insert into books
    (pid,
    name,
    pic,
    list_price,
    lour_price,
    description

    select
    pid,
    name,
    pic,
    list_price,
    lour_price,
    description
    from temp
    where status=1
    limit $l_num,100";
    mysql_query($squery)
    or die("13$squery:".mysql_error());
    }
    ///insert into c_p
    $c_pqur="select id from c_p where status=0" ;
    $c_pr=mysql_query($c_pqur) or die("20:$c_pqur".mysql_error());
    $c_prow=mysql_num_rows($c_pr);
    if($c_prow>=$j){
    $t_result2=mysql_query("select pid, cid from temp where status=1") or die(mysql_error());

    for($c_pi=0;$c_pi<$j;$c_pi++){
    $t_obj=mysql_fetch_object($t_result2);
    $cid=$t_obj->cid;
    $pid=$t_obj->pid;
    $c_pobj=mysql_fetch_object($c_pr);
    $c_pid=$c_pobj->id;
    $c_pup="update c_p set cid='$cid',pid='$pid',status=1 where status=0 and id='$c_pid'";
    mysql_query($c_pup) or die("21:$c_pup".mysql_error());
    }
    }
    else
    {
    $t_result2=mysql_query("select pid, cid from temp where status=1") or die(mysql_error());

    for($pci=0;$pci<$c_prow;$pci++){
    $t_obj=mysql_fetch_object($t_result2);
    $cid=$t_obj->cid;
    $pid=$t_obj->pid;
    $c_pobj=mysql_fetch_object($c_pr);
    $c_pid=$c_pobj->id;
    $c_pup1="update c_p set cid='$cid',pid='$pid',status=1 where status=0 and id='$c_pid'";
    mysql_query($c_pup1) or die("22:$c_pup1".mysql_error());
    }
    $cp_num=$k;
    $c_pin="insert into c_p(pid,cid) select pid,cid from temp where status=1 limit $cp_num,100";
    mysql_query($c_pin) or die("23:$c_pin".mysql_error());
    }
    插入产品和产品和产品目录关系表的部分
      

  2.   

    while($t_obj=mysql_fetch_object($t_result1))
    {
    $name=$t_obj->name;
    $name=str_replace("/"," ",$name);$name=str_replace("\\"," ",$name);
    $name=str_replace("_"," ",$name);$name=str_replace("-"," ",$name);
    $name=str_replace("+"," ",$name);$name=str_replace("("," ",$name);
    $name=str_replace(")"," ",$name);
    $n=explode(" ",$name);
    $m=array_unique($n);
    foreach($m as $lin)
    {
    if(ereg("^[a-zA-Z]+$",$lin)&& strlen($lin)>2)
    { echo $lin.'<br>';
    $w_query="select name,count from words where name='$lin'";
    $w_r=mysql_query($w_query) or die("21:$w_query".mysql_error());
    $w_obj=mysql_fetch_object($w_r);$count=$w_obj->count;
    if(mysql_num_rows($w_r)==0)
    {
    mysql_query("insert into words(name,count) values('$lin',1)")or die("5:".mysql_error());
    $w_query1="select wid from words where name='$lin'";
    $w_r1=mysql_query($w_query1)or die("22:$w_query1".mysql_error());

    }
    else
    {
    $count=$count+1;
    $w_que="update words set count='$count' where name='$lin'";
    $w_qr=mysql_query($w_que) or die ("24::$w_qr".mysql_error());
    $w_query1="select wid from words where name='$lin'";
    $w_r1=mysql_query($w_query1)or die("23:$w_query1".mysql_error());
    }
    $t_result3=mysql_query("select pid, cid from temp where status=1 ") or die(mysql_error());
    $w_obj=mysql_fetch_object($w_r1);$wid=$w_obj->wid;
    $t_obj3=mysql_fetch_object($t_result3);
    $cid3=$t_obj3->cid;
    $pid3=$t_obj3->pid;
    $w_cqu="select id from w_c where status=0";
    $w_cre=mysql_query($w_cqu) or die("30:$w_cqu".mysql_error());
    $w_cnum=mysql_num_rows($w_cre);
    $w_pqu="select id from w_p where status=0";
    $w_pre=mysql_query($w_pqu) or die("30:$w_cqu".mysql_error());
    $w_pnum=mysql_num_rows($w_pre);
    if($w_cnum==0){
    $w_cin="insert into w_c(wid,cid) values('$wid','$cid3')";
    mysql_query($w_cin) or die("6:".mysql_error());
    }
    else
    {
    echo $w_cnum;
    $w_cobj=mysql_fetch_object($w_cre);$w_id=$w_cobj->id;
    $w_cup="update w_c set wid='".$w_cobj->id."',cid='$cid3',status='1' and id='$w_id'";
    mysql_query($w_cup) or die ("30:$w_cup".mysql_error());
    //$w_cnum=$w_cnum-1;
    }
    if($w_pnum==0){
    $w_pin="insert into w_p(wid,pid) values('$wid','$pid3')";
    mysql_query($w_pin) or die("6:".mysql_error());
    }
    else
    {
    //echo $w_pnum;
    $w_pobj=mysql_fetch_object($w_pre);$w_id=$w_pobj->id;
    $w_pup="update w_p set wid='".$w_pobj->id."',pid='$pid3',status='1' where id='$w_id'";
    mysql_query($w_pup) or die ("30:$w_pup".mysql_error());
    //$w_pnum=$w_pnum-1;
    }
    }
    }
    } echo "insert sucessfully!";
    fclose($handle);
    unlink($uploadfile);
    //mysql_query("drop table temp");
    mysql_close();
    //echo "<meta http-equiv=refresh content=3;URL=' '>";
    }
    else{
    print("<html>
    <head>
    <title>商品录入平台</title>
    </head>
    <body>
    <center>
    <form enctype=multipart/form-data action='' method=post>
    <input type=hidden name=MAX_FILE_SIZE value=300000>
    Upload files: <input name=userfile type=file>
    <input type=submit value=upload>
    </form>
    </center>
    </body>
    </html>");
    }
    function conts($line,$args1,$args2)//自定义函数取$line中夹在$args1和$args2之间的字符串
    {
        if ($args1!="") $pos1=strpos($line,$args1)+strlen($args1);
        else $pos1=0;
        if ($args2!="") $pos2=strpos($line,$args2);
        else $pos2=strlen($line);
        $pos2 = $pos2 - $pos1;
        return trim(substr($line,$pos1,$pos2));
    }
    ?>
    最后这部分有问题,插入单词表,并插入单词产品关系表和单词产品目录关系表,麻烦大家帮我看看,弄了几天,被搞到哭了,都没解决
      

  3.   

    这么长,能看的下去吗,把不行的那句贴出来把关键的if else 贴出来如这个if($w_pnum==0){
       echo "111";
       $w_pin="insert into w_p(wid,pid) values('$wid','$pid3')";
      mysql_query($w_pin) or die("6:".mysql_error());
    }
    else
    {
    //echo $w_pnum;
           echo "222";
        $w_pobj=mysql_fetch_object($w_pre);$w_id=$w_pobj->id;
        $w_pup="update w_p set wid='".$w_pobj->id."',pid='$pid3',status='1' where   
        id='$w_id'";
        echo  $w_pup; //打印SQL 语句    mysql_query($w_pup) or die ("30:$w_pup".mysql_error());
        //$w_pnum=$w_pnum-1;
    }
    如果打印出222
    在看看打印的SQL 语句,
    把SQL 语句单独提出来  放到数据库里看看能不能执行
    不过你的SQL 语句好象有点问题
    $w_pup="update w_p set wid='".$w_pobj->id."',pid='$pid3',status='1' where   
        id='$w_id'";
    改成$w_pup="update w_p set wid='".$w_pobj->id."',pid='".$pid3."',status='1' where   
        id='".$w_id."'";
      

  4.   

    $w_pin="insert into w_p(wid,pid) values('$wid','$pid3')";
    这个插入能正确执行吗?SQL 语句语句实际上就是字符串的连接;$w_pin="insert into w_p(wid,pid) values('$wid','$pid3')";如 insert into w_p(wid,pid) values('123','234') 这句  "insert into w_p(wid,pid) values('"  .  $wid  . "','"  . $pid3  ."')". 号是连接,""间的部分就是 SQL 语句,$wid="123" ,$pid3="234"
    连接后就是 insert into w_p(wid,pid) values('123','234')
      

  5.   

    if($w_cnum==0){
    $w_cin="insert into w_c(wid,cid) values('$wid','$cid3')";
    mysql_query($w_cin) or die("6:".mysql_error());
    }
    else
    {
    echo $w_cnum;
    $w_cobj=mysql_fetch_object($w_cre);$w_id=$w_cobj->id;
    $w_cup="update w_c set wid='".$w_cobj->id."',cid='$cid3',status='1' and id='$w_id'";
    mysql_query($w_cup) or die ("30:$w_cup".mysql_error());
    //$w_cnum=$w_cnum-1;
    }
    if($w_pnum==0){
    $w_pin="insert into w_p(wid,pid) values('$wid','$pid3')";
    mysql_query($w_pin) or die("6:".mysql_error());
    }
    else
    {
    //echo $w_pnum;
    $w_pobj=mysql_fetch_object($w_pre);$w_id=$w_pobj->id;
    $w_pup="update w_p set wid='".$w_pobj->id."',pid='$pid3',status='1' where id='$w_id'";
    mysql_query($w_pup) or die ("30:$w_pup".mysql_error());
    //$w_pnum=$w_pnum-1;
    }
    }
    关键是判断这个检查出的字段是否为0的时候,如果否,就执行更新,但是更新不了
      

  6.   

    if($w_pnum==0){
       echo "111";
       $w_pin="insert into w_p(wid,pid) values('$wid','$pid3')";
      mysql_query($w_pin) or die("6:".mysql_error());
    }
    else
    {
    //echo $w_pnum;
           echo "222";
        $w_pobj=mysql_fetch_object($w_pre);$w_id=$w_pobj->id;
        $w_pup="update w_p set wid='".$w_pobj->id."',pid='$pid3',status='1' where   
        id='$w_id'";
        echo  $w_pup; //打印SQL 语句    mysql_query($w_pup) or die ("30:$w_pup".mysql_error());
        //$w_pnum=$w_pnum-1;
    }
    如果打印出222
    在看看打印的SQL 语句,
    把SQL 语句单独提出来  放到数据库里看看能不能执行你首先要看进没进  else 里去
    如果进去了再把SQL 语句打印 放到数据库里看看能不能执行要一步一步找出错误在哪
      

  7.   


    更新不了 为什么呢?
    是没进 else  里面去,还是SQL 语句有问题,还是SQL 语句执行了没有commit,还是其他,要一步一不排除
      

  8.   

    在把我前面的仔细看看,我估计SQL有问题
      

  9.   

    但是sql没什么问题,else里面不能运行