Solve the problem of duplicate records when uploading files

solution

1. Import the data, and then solve the problem of duplicate student numbers. Store all the student numbers in the original table into an array, and then use getField to obtain them.

2. Read a row of data in the file and check whether the student number in the changed row exists in the array using the in_array function. If it does not exist, write it into the two-dimensional array $arr,

And append the student number to arrno; if it exists, continue reading the next line.

Implementation code example:


$fp = fopen($file,'r');
    if($fp){
      $fields = array('no','name','sex');
      $model = M('newstudent');
       $arrNo = $model->getField('no',true);

      $arr = array();

      while(($row=fgetcsv($fp,1000,","))!==false){
        $row = array_combine($fields, $row);
        if(in_array($row['no'], $arrNo)){
              echo $row['no'].'存在'.PHP_EOL;
            }else{
              $arrNo[]=$row['no'];
              $arr[]=$row;
              echo $row['no'].'导入成功'.PHP_EOL;
            }


Guess you like

Origin blog.csdn.net/TIANJING_er/article/details/79725762