加载数据Infile,将csv导入mysql

时间:2015-10-21 15:52:53

标签: php sql csv file-io

我有一个包含以下数据的.csv文件:

BookingNumber;AccountCode;NumberOfVolumes;DeclaredWeight;DeclaredVolume;ShippedOn;SenderName;AddresseeName;AddresseeStreet;AddresseeLocation;AddresseePostalCode;AddresseeCountryUNCode;InsuredValue;DeclaredValue;ShipperReferences;ServiceCode;StatusCode;Incidence;ChargeAmount;Observations;PTATCode;ReturnDeliveryNote;ReimbursementMethod;ReimbursementMethodDescription;ReimbursementProtocolNumber;
'2#####0;X#####;1;'60,00;;'2015-10-14;XXXXXXXXXX;XXXXXXXXXX;XXXXXXXXXX ##;XXXXXXXXXXXXXX;'2430;PT;False;'0,00;;XX####;DELIVERY;False;'0,00;;;False;;;;

看起来这是我的.CSV,但之后有更多的数据行,我不得不更换一些数据(X代表字母,#代表数字,因为它对公司敏感)。

我有一个简单的上传脚本:

include('connect.php');

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Check if file already exists
/*if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}*/
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "csv") {
    echo "Sorry, only .CSV files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

这个带有LOAD DATA INFILE的代码示例将这些行插入我的MySQL数据库。我只需要一些选定的行,但我无法将数据插入到BD中,我不知道为什么,它上传文件并且在查询中没有错误,所以我在这里有点丢失。我的问题是,我在这里做错了什么?

$sql="LOAD DATA INFILE './20151015035532 - Exp.csv' 
    INTO TABLE despacho
    FIELDS TERMINATED BY ';' 
    LINES TERMINATED BY ';\r\n'
    IGNORE 1 LINES
    (@BookingNumber,@AccCode,@NumberOfVolumes,@DeclaredWeight,@DeclaredVolume,@ShippedON,@SenderName,@ReceptorName,@ReceptorAdress,@ReceptorLocation,@ReceptorCodigoPostal,@ReceptorCountryCode,@InsuredValue,@DeclaredValue,@ShipperReferences,@ServiceCode,@StatusCode,@Incidence,@ChargeAmount,@Obs,@PTATCode,@ReturnDeliveryNote,@Reimbursement1,@Reimbursement2,@Reibursement3) 
    SET BookingID = TRIM(BOTH '\'' FROM @BookingNumber,
        NumeroVolumes = TRIM(BOTH '\'' FROM @NumberOfVolumes,
        Peso = TRIM(BOTH '\'' FROM @DeclaredWeight,
        DeclaredVolume = NULLIF(@DeclaredVolume,''),
        DataEnviado = STR_TO_DATE(TRIM(BOTH '\'' FROm @ShippedON),'%Y-%m-%d'),
        NomeEnvio = @ReceptorName,
        MoradaEnvio = @ReceptorAdress,
        LocalEnvio = @ReceptorLocation,
        CPEnvio = TRIM(BOTH '\'' FROM @ReceptorCodigoPostal),
        CodPaisEnvio = @ReceptorCountryCode,
        ReferenciasEnvio = TRIM(BOTH '\'' FROM @ShipperReferences),
        CodigoEstado = @StatusCode,
        ValorCobranca = TRIM(BOTH '\'' FROM @ChargeAmount)";

$res = mysqli_query($conn,$sql);

不用担心安全问题,因为服务器和客户端都在我们的本地网络上,无法从外部访问。

0 个答案:

没有答案