我用uploadify上传文件。后台用PHP 控制。为什么会插入两次数据?其中有一次是没有文件信息的?请高手指教。谢了。

解决方案 »

  1.   

    <?php
    session_start();
    header("content-type:text/html;charset=utf-8");
    include("../includes/conn.php");
    mysql_query("SET NAMES UTF8");
    if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    //$fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
    // $fileParts  = pathinfo($_FILES['Filedata']['name']);
    $fileParts  = pathinfo($_FILES['Filedata']['name']);
    $filetype=$fileParts['extension'];
    $time=time();
    $targetFile =  str_replace('//','/',$targetPath) . $time.".".$filetype;


    // $fileTypes  = str_replace(';','|',$fileTypes);
    // $typesArray = split('\|',$fileTypes);
    // if (in_array($fileParts['extension'],$typesArray)) {
    // Uncomment the following line if you want to make the directory if it doesn't exist
    // mkdir(str_replace('//','/',$targetPath), 0755, true);

    move_uploaded_file($tempFile,$targetFile);
    // echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
    // } else {
    //  echo 'Invalid file type.';
    // }$up_name=$_SESSION['oname'];
    $date=date("Y-m-d");
    mysql_select_db("doc");
    $query="UPDATE  users  SET up_num = up_num + 1 WHERE username='$up_name'";
    $result=mysql_query($query)or die(mysql_error());
    $query="INSERT INTO doc(up_name,class,name,miaoshu,date,tmp_name,ext,keyword,sell)"."VALUES('$up_name','$class','$name','$describe','$date','$file_name','$ftype','$keyword','$sell')";
    $result=mysql_query($query)or die(mysql_error());
    $id=mysql_insert_id();
    if($id>0){
    echo "<script>alert('用户[".$up_name."],上传文档成功,请继续上传!');location.href='upfile.php';</script>";
    }else{
    echo "<script>alert('上传失败,请重新上传!');location.href='upfile.php';</script>";
    }}
    ?>这是我的后台PHP 代码。。
      

  2.   

    是不是你那两句SQL语句赋值变量重复了。 
    最后那个$query换个名字试试。
      

  3.   

    自己已解决。原来uploadify 有个后台 自己会运行一次。我提交表单又运行一次