简体   繁体   中英

PHP import csv not working

I have a problem which bothers me for the past few days. I searched and tried everything, but till now, nothing worked.

In short: I have a csv upload which is working fine, but the data import itself is not working.

This is the full script (security is no issue since only certain people can acces this anyway, but feel free to add suggestions for it):

if(isset($_POST['athene_advanced_submit_csv'])){
$admin_valid = true; //not doing anything yet

if($admin_valid == true){
    $filename = $_FILES['athene_files']['name'];
    $target_path = '../db/';
    $result_files = move_uploaded_file($_FILES['athene_files']['tmp_name'], $target_path.$filename);


    if($result_files == true){
        $query_import = "LOAD DATA INFILE 'import.csv'
        INTO TABLE `test`
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY ',,,\r\n'
        (id, name, price)";

        $result_import = mysqli_query($dbcon, $query_import);

        if($result_import == true){
            echo '<script>alert("CSV imported");</script>';     
        }else{
            echo '<script>alert("import error");</script>';     
        }
    }else{
        echo '<script>alert("file error");</script>';   
    }

}
else{
    echo '<script>alert("no file");</script>';  
}

}

It returns "Import error" over and over.

my csv is as following:

id,name,price
1,one,12
2,two,23
3,three,34
4,four,45
5,five,56
6,six,67
7,seven,78

Can somebody help me? I think the problem is that my query is not suitable for the export of my csv, but haven't found a solution yet.

Very many thanks in advance!

Can you try:

LOAD DATA LOCAL INFILE '/your/absolute/path/import.csv' INTO TABLE `test`
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n'  ## if windows '\r\n' 
    IGNORE 1 LINES
    (id, name, price)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM