使用PHP无法将CSV导入MYSQL

时间:2018-04-02 06:28:58

标签: php mysql csv

我正在尝试将CS​​V文件导入MySQL我在第4行上遇到错误我知道这必须是一个简单的问题,但我以前从未这样做过......

这是我的代码。

<?php
    $file = fopen("http://*******.com/*****/********.csv", "r");
    $servername = "*******";
    $username = "*************";
    $password = "**************";
    $dbname = "*********";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    }
    while (($line = fgetcsv($file, 0, ",")) !== FALSE){
        echo $sql = "INSERT INTO inventory_two (
            `Add_City_State_Zip`,
            `CommentsInstalledOptions`,
            `TodaysDate`,
            `total_Acquired_Service`,
            `total_Acquired_Service_PakFee`,
            `total_Service`,
            `year_make_model_trim`,
            `AcquiredDate`,
            `AcquiredPrice`,
            `ACV`,
            `AdditionalNotes`,
            `AltVehicleLocation`,
            `ASIS`,
            `AskDown`,
            `Askprice`,
            `AskPrice_Low`,
            `AskTerm`,
            `AutoBoingURL`,
            `BodyStyle`,
            `BodyStyle_Ebay`,
            `ClearTitle`,
            `CommentOptionsFuelStereo`,
            `Comments`,
            `Condition_ID`,
            `ConditionDesc`,
            `DaysInInventory`,
            `DriveType`,
            `Engine`,
            `ESN`,
            `ExtColor`,
            `ExtTrim`,
            `Flags`,
            `FuelType`,
            `Images`,
            `Images_Primary`,
            `ImageUpdate_Epoc`,
            `Inspected`,
            `Inst_Add1`,
            `Inst_City`,
            `Inst_DBA`,
            `Inst_Email`,
            `Inst_ID`,
            `Inst_IDLot_ID`,
            `Inst_Name`,
            `Inst_Phone1`,
            `Inst_State`,
            `Inst_Website`,
            `Inst_Zip`,
            `InstalledOptions`,
            `IntColor`,
            `Inventory_ID`,
            `InvType_ID`,
            `InvTypeDesc`,
            `Lot_Add1`,
            `Lot_Add2`,
            `Lot_City`,
            `Lot_Email`,
            `Lot_ID`,
            `Lot_Phone1`,
            `Lot_Phone2`,
            `Lot_State`,
            `Lot_Zip`,
            `LotLegalName`,
            `LotLocation`,
            `Make`,
            `Mileage`,
            `MileageStatus_ID`,
            `Model`,
            `NewFlag`,
            `PakFee`,
            `Provider_ID`,
            `ProviderCode`,
            `SellerNotes`,
            `Status`,
            `Stereo`,
            `StockNumber`,
            `TitleLocation`,
            `TitleStatus_ID`,
            `TotalExpenses`,
            `Transmission`,
            `Transmission_Common`,
            `UserDefined1`,
            `VIN`,
            `WarrantyTerms`,
            `Weight`,
            `WholeSalePrice`,
            `Year`
        )
        VALUES ($line[0]','$line[1]','$line[2]','$line[3]','$line[4]','$line[5]','$line[6]','$line[7]','$line[8]','$line[9]','$line[10]','$line[11]','$line[12]','$line[13]','$line[14]','$line[15]','$line[16]','$line[17]','$line[18]','$line[19]','$line[20]','$line[21]','$line[22]','$line[23]','$line[24]','$line[25]','$line[26]','$line[27]','$line[28]','$line[29]','$line[30]','$line[31]','$line[32]','$line[33]','$line[34]','$line[35]','$line[36]','$line[37]','$line[38]','$line[39]','$line[40]','$line[41]','$line[42]','$line[43]','$line[44]','$line[45]','$line[46]','$line[47]','$line[48]','$line[49]','$line[50]','$line[51]','$line[52]','$line[53]','$line[54]','$line[55]','$line[56]','$line[57]','$line[58]','$line[59]','$line[60]','$line[61]','$line[62]','$line[63]','$line[64]','$line[65]','$line[66]','$line[67]','$line[68]','$line[69]','$line[70]','$line[71]','$line[72]','$line[73]','$line[74]','$line[75]','$line[76]','$line[77]','$line[78]','$line[79]','$line[80]','$line[81]','$line[82]','$line[83]','$line[84]','$line[85]','$line[86])";
        if ($conn->query($sql) === TRUE){
            echo "New record created successfully";
        }
        else{
            echo "Error: " . $sql . "<br />" . $conn->error;
        }
    }
    fclose($file);
?>

如果我在while循环中回显$line[] items,则所有回声都正确。所以我不明白为什么这不起作用?

0 个答案:

没有答案