问题:使用phpMyAdmin Works导入csv,Php脚本以不同方式处理csv导入

时间:2015-09-03 12:28:11

标签: php csv import phpmyadmin

我一直在使用这个脚本,我相信这有点过时了。我遇到的问题是,当我使用phpMyAdmin导入csv文件(6000多条记录)时,它可以正常工作,但是,当我使用这个php脚本导入相同的csv文件时,它会破坏字段顺序。

仅导入前两个记录。导入的第二个记录无法正确导入,因为第二个记录的第一个字段包含在第一个记录的最后一个字段中,因此导入过程似乎无法识别下一个记录的换行符。我尝试将csv文件保存为utf8,以及ascii。无论哪种情况都没有区别。

修改脚本以处理csv文件的任何想法都与phpMyAdmin一样吗?

<?php

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

$deleterecords = "TRUNCATE TABLE plfwinvtable"; //empty the table of its current records

mysqli_query( $conn, $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 plfwinvtable(Barcode, Brand, Description, Category, Price, Size, Unit, ServeOz, NumDrinks, NameVar) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')";

        mysqli_query($conn, $import) or die(mysqli_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>";

}

?>

1 个答案:

答案 0 :(得分:0)

将文件另存为 upload.php

<html>
<head>
<style type="text/css">
body {
    background: #E3F4FC;
    font: normal 14px/30px Helvetica, Arial, sans-serif;
    color: #2b2b2b;
}
a {
    color:#898989;
    font-size:14px;
    font-weight:bold;
    text-decoration:none;
}
a:hover {
    color:#CC0033;
}

h1 {
    font: bold 14px Helvetica, Arial, sans-serif;
    color: #CC0033;
}
h2 {
    font: bold 14px Helvetica, Arial, sans-serif;
    color: #898989;
}
#container {
    background: #CCC;
    margin: 100px auto;
    width: 945px;
}
#form           {padding: 20px 150px;}
#form input     {margin-bottom: 20px;}
</style>
</head>
<body>
<div id="container">
<div id="form">

<?php

include "inc/database.php"; //Connect to Database

//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 awards (field1, field2, field3, field4) values('$data[0]','$data[1]','$data[2]','$data[3]')";

        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>";

}

?>

</div>
</div>
</body>
</html>

将100%工作......:)

enter image description here

enter image description here

enter image description here

enter image description here