警告csv上传中的重复记录

时间:2015-05-29 05:42:02

标签: php csv

我的csv上传代码就像这样想要提醒已经存在于数据库中的重复记录我正在获得带有$ status变量的结果我该怎么做才能提醒每个重复记录提醒每个重复记录的方法

if(isset($_POST["Import"]))
{
  $filename=$_FILES["file"]["tmp_name"];
  if($_FILES["file"]["size"] > 0)
  { 

    $row = 1;
    if (($handle = fopen("$filename", "r")) !== FALSE) 
    {
        while (($data = fgetcsv($handle)) !== FALSE) 
        {
            $num = count($data);

            $row++;
            for ($c=0; $c < $num; $c++) {

                  if($c!="" && $c+1!="" && $c+2!=""){              
                    $a[] =  $data[$c];

                  }          
            }
        }
        $total = count($a);
        $m=0;
        $k = 1;
        for ($c=0; $c < $total; $c++) 
        {

           $address[$m][] = $a[$c] ;
           $z  = $address[$m][0];
           $q  = $address[$m][1];
           $q .= $address[$m][2];
           $q .= $address[$m][3];
           $q .= $address[$m][4];
           $q .= $address[$m][5];
           $q .= $address[$m][6];
           $q .= $address[$m][7];
           $q .= $address[$m][8];
           $q .= $address[$m][9];
           if($a[$c]=="")
           {
              if($k!=1)
              {

                $selectcon = "SELECT user_id FROM contact
                                WHERE user_id = '".$z."'";

                $selectRes = mysql_query($selectcon);
                if($rows = mysql_fetch_array($selectRes))
                {
                  $user_id = $rows['user_id'];

                  if($user_id == $z)
                  {
                    $staus = 1;
                  }
                  else
                  {
                    $staus = 1;
                  }
                }

                  $insertParty = "INSERT INTO contact(user_id)values('$z')";
                  $res = mysql_query($insertParty);
                  $contact_id = mysql_insert_id();


                  $insertAddress = "INSERT INTO address(contact_id,address)VALUES($contact_id,'$q')";
                  $insertAddressRes = mysql_query($insertAddress);
                  if(!$insertAddressRes)
                  {
                    echo "<sctipt>";
                    echo "sweetAlert('oops','Import Data Fail','error')";
                    echo "</sctipt>";
                  }
                  else
                  {
                     echo "<script>";
                     echo "swal('Sucess!', 'File Imported Sucessfully!','success')";
                     echo "</script>";
                  }
                  $m++;
                }
                $k++;

          }

        }
      fclose($handle);
    }
  }
}
?>

1 个答案:

答案 0 :(得分:1)

您需要在数据库中创建一个唯一的行。你不能用PHP做到这一点。

将所有列添加到单个UNIQUE索引中,您无法在该表中插入重复的行。

相关问题