解析错误PHP:使用数据库将CSV文件导入mysql

时间:2017-08-21 12:59:30

标签: php html mysqli

解决了:我错过了;在echo文件中

我是php的新手,我已经使用html中的输入文本框成功将数据加载到sql中,但是我无法通过读取csv文件将其加载到数据库中,并且我正在为fopen函数获取解析错误。请帮忙

upload.php的

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ib";
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else
        $file=$_FILES["file"]["tmp_name"];

    echo $file
    fopen($file, 'r');
    while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
         $sql = "INSERT into ib(atmid,location,zone,cash,fault) values('.$emapData[0]','.$emapData[1]','.$emapData[2]','.$emapData[3]','.$emapData[4])";
        $conn->query($sql);
    }
    fclose($test);
    echo "CSV File has been successfully Imported.";
?>

1 个答案:

答案 0 :(得分:2)

$sql有引号问题,下面是更新的问题:

 $sql = "INSERT into ib(atmid,location,zone,cash,fault) values('".$emapData[0] ."','".$emapData[1] ."','" . $emapData[2] . "','".$emapData[3] . "','".$emapData[4] . "')";