php插入查询重复?

时间:2016-12-27 16:57:33

标签: php mysql

你们都可以看到这是一个从csv文件中读取数据并插入数据库的代码但是当我运行这段代码时,查询是插入1个数据2次请告诉我这里做错了什么建议谢谢你...

  <?php
   include 'db.php';
   // defined a variable header
    $header = NULL;
     // define variable as array
   $data = array();
  // checking if fopen is not false
 if (($handle = fopen('Book1.csv', 'r')) !== FALSE)
     {    // loop to
    while (($row = fgetcsv($handle, 100, ",")) !== FALSE)
    { 
        //print_r($row);
        //DIE();
        if(!$header)
        {
            $header = $row;
        }
        else
        {
            $data[] = array_combine($header, $row); //array_merge($header, $row);  
           if(is_array($data))
           {
                print_r($data);
          //      die();
               $valuesarray = array();
               foreach($data as $data1)
               {

                $name   = $data1['name'];
                $age    = $data1['age'];
                $dpt    = $data1['dpt'];
                $salary = $data1['salary'];

                $valuesarray[] = "('.$name.','.$age.','.$dpt.','.$salary.')";


                }
                 $sql = "INSERT INTO excelfile(name ,age, dpt, salary) values ";     



               //}
               //die();

               $sql.= implode(',', $valuesarray);
               //print_r($sql); 
               $is_query_run = mysql_query($sql);
               echo "Data added";
          }
           else
           {
            echo "not in the array";
           }
        // echo "<pre>";
        // print_r($data);
        // echo "</pre>";


       }

    }
    fclose($handle);
}
return $data;

0 个答案:

没有答案