2016-09-09 105 views
1

所以我出了一點我的深度的位置(再一個),並需要在其導入到我的數據庫刪除從CSV數據集的單引號...從PHP CSV導入去掉'撇號

的代碼我目前所面對的讀取,所以

<?php 

include "connection.php"; //Connect to Database 

$deleterecords = "TRUNCATE TABLE riders_long"; //empty the table of its current records 
mysql_query($deleterecords); 

//Upload File 
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 into riders_long(Onlineentry,SaleID,FirstName,LastName,ClubTeamName,DOB,IsBCMember,MemberNo,Agreetermsandconditions,Agreeemailconfirmation,Agreeemailmarketing,LicenceCategory,Gender,Email,Daytimephone,Address1,Address2,TownCity,Region,Country,Postcode,EmergencyContactName,EmergencyContactPhoneNumber,DatePaid,AmountPaid,RefundAmount,PaymentType,Status) 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]')"; 

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

    fclose($handle); 

    print "Import done"; 

    //view upload form 
}else { 

    print "Upload new csv by browsing to file and clicking on Upload<br />\n"; 

    print "<form enctype='multipart/form-data' action='upload.php' method='post'>"; 

    print "File name to import:<br />\n"; 

    print "<input size='50' type='file' name='filename'><br />\n"; 

    print "<input type='submit' name='submit' value='Upload'></form>"; 

} 

?> 

如果我說實話,我甚至不知道從哪裏開始與這一個...

我相信我需要使用str_replace()功能...東西沿線

然後
$remove = array("'"); 
$filtered = str_replace($remove, "", "Hello World of PHP"); 

$filtered將字符串沒有「撇號,但隨後我有點困惑我會如何以及在何處最好的整合,從哪裏開始會是這樣

建議非常讚賞

+0

當然你真正需要做的是不刪除撇號,這是逃避他們,使他們不會破壞你的SQL INSERT語句 –

+0

如果SER ver允許它可以直接使用某些東西導入文件作爲http://stackoverflow.com/questions/11077801/import-csv-to-mysql-table/36586798中的答案,或者您可以使用mysqli_或PDO而不是棄用mysql_函數並使用綁定變量。 (用於查詢類型的舊方法是在所有字段('mysql_real_escape_string($ data [5])')周圍使用'mysql_real_escape_string()') – rypskar

回答

1

使用file_get_contents ,某事像這樣:

$data = file_get_contents(...); 
$cleanData = str_replace("'","",$data);