CSV上传 - 跳过具有特殊字符的行

时间:2018-03-01 06:50:35

标签: php csv mysqli

我正在将CSV文件导入mysql DB,如果行包含特殊字符,例如" '"它会跳过那一行并且不会导入。请帮我导入行 所有特殊字符或导入行不带特殊字符。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<?php

header('Content-Type: text/html; charset=UTF-8');

 if(isset($_POST["Import"])){

         $filename=$_FILES["file"]["tmp_name"];     

         if($_FILES["file"]["size"] > 0)
         {
            $file = fopen($filename, "r");
            $i=0;
            while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
             {

                $getData = array_map("utf8_encode", $getData); 
                if($i==0){$i++;continue;}
                $sql = "INSERT into do_order (tracking_id,                                      
                no_of_attempts                       
                ) 
                   values ('".$getData[0]."',                          
                   '".$getData[41]."'               
                   )";

                   $result=$db_handle->executeUpdate($sql);
                if(!isset($result))
                {
                    echo "Please Upload CSV File";      
                }
                else {
                      echo "csv imported";
                }
                $i++;
             }

             fclose($file); 
         }
    }    
echo "alert message";

?>

更新我正在使用php 7.025,也尝试使用 mysqli_real_escape_string ,如下代码:

<?php

$conn = mysqli_connect("localhost","root","","ecom");
require_once("dbcontroller.php");
$db_handle = new DBController();

header('Content-Type: text/html; charset=UTF-8');

 if(isset($_POST["Import"])){

         $filename=$_FILES["file"]["tmp_name"];     


         if($_FILES["file"]["size"] > 0)
         {
            $file = fopen($filename, "r");
            $i=0;
            while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
             {

                $getData = array_map("utf8_encode", $getData); //added

                //echo mb_convert_encoding($getData[$file],"HTML-ENTITIES","UTF-8");

                if($i==0){$i++;continue;}


                $sql = "INSERT into do_order (tracking_id,

                telecomments                
                ) 

                   values ('".mysqli_real_escape_string($conn , $getData[0])."',

                   '".mysqli_real_escape_string($conn , $getData[45])."'
                   )";

                   $result=$db_handle->executeUpdate($sql);



                if(!isset($result))
                {
                    echo "";        
                }
                else {
                      echo "";
                }
                $i++;
             }

             fclose($file); 
         }
    }    
echo "";
?>

here,您可以下载罪魁祸首CSV行

这个问题不重复......

0 个答案:

没有答案