2015-06-30 227 views
-2

我們可以閱讀和從CSV文件中刪除單引號,雙引號和分號每一列,以便您可以上傳CSV文件到MySQL沒有任何錯誤閱讀CSV文件在PHP

if (isset($_POST['submit'])) { 
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) { 
     echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>"; 
     echo "<h2>Displaying contents:</h2>"; 
     readfile($_FILES['filename']['tmp_name']); 
    } 

    //Import uploaded file to Database 
    $handle = fopen($_FILES['filename']['tmp_name'], "r"); 

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $import="INSERT IGNORE INTO brokers (id,btype,bname,bphone,bmobphone,bfax,bemail,bwebsite,bcompany,bintrowho,bcurrentwho,bintrowhat,baddress,bjobtitle,bregion,bcity,bstate,bzip,bsweetspot1,bsweetspot2,bsweetspot3,bsweetspot4,bsweetspot5,bsweetspot6,bsweetspot7,bsweetspot8,bsweetspot9,bsweetspot10,bsweetspot11,bsweetspot12,bsweetspot13,bsweetspot14,bsweetspot15,baltphone,bdepartment,bitr,bvrr,bskype,bhangouts,bcomments) VALUES ('','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]','$data[22]','$data[23]','$data[24]','$data[25]','$data[26]','$data[27]','$data[28]','$data[29]','$data[30]','$data[31]','$data[32]','$data[33]','$data[34]','$data[35]','$data[36]','$data[37]','$data[38]')"; 

     mysql_query($import) or die(mysql_error()); 
    } 

    fclose($handle); 

    print "Import done"; 
$import=header("location:User_option.php"); 
    //view upload form 
} 
+1

是可能的。我已經實現了它,但社區的規則是清晰的 - 您編寫代碼並儘可能擴展我們的支持。我們無法爲您編寫代碼,因此請先谷歌並自行嘗試! – CrakC

+0

好的抱歉,我的代碼是 –

+0

請發佈代碼。 – DickieBoy

回答

0

什麼你正在這裏SQL injection。這是一種安全性的保證。

基本而言,您應該將mysql_real_escape_string放在您正在使用的data[N]附近。例如:

"INSERT IGNORE INTO brokers (id,btype) VALUES ('','mysql_real_escape_string($data[0])')" 

瞭解詳情,請閱讀thisthis