2016-09-09 57 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);