CSV文件格式

时间:2015-11-18 17:33:10

标签: php csv ms-access export-to-csv

我遇到问题:我需要能够使用PHP将CSV文件导入MS Access数据库。我有一个可以执行此操作的脚本,但我的CSV文件格式不正确。

原始格式为: Original format

但脚本适用于以下格式: needed format

这两个文件都是.csv。如何将第一种文件格式转换为所需的格式?

下面我的导入脚本没有导入任何内容,并且没有给出任何错误:

if (isset($_POST['submit'])) {
            $i=0;
            require "connection.php";
            if (is_uploaded_file($_FILES['csv']['tmp_name'])) {
                echo "<h3>" . "File ". $_FILES['csv']['name'] ." uploaded successfully." . "</h3>";
            }
            //Import uploaded file to Database
            $handle = fopen($_FILES['csv']['tmp_name'], "r");
            $import=$db->prepare("INSERT INTO adherence(
                    dateandtime,
                    lastname,
                    firstname,
                    paidtime,
                    approvedtime,
                    notadhering)VALUES(
                    ?,?,?,?,?,?)");
            while (($data = fgetcsv($handle, 1000,"\t","'")) !== FALSE) {
                if($i>0) {
                $data = str_replace('"', '', $data);
                $myDate =  date("Y/m/d",strtotime(str_replace('/','-',$data[0])));
                $import->bindParam(1, $myDate, PDO::PARAM_STR);
                $import->bindParam(2, $data[1], PDO::PARAM_STR);
                $import->bindParam(3, $data[2], PDO::PARAM_STR);
                $import->bindParam(4, $data[3], PDO::PARAM_STR);
                $import->bindParam(5, $data[4], PDO::PARAM_STR);
                $import->bindParam(6, $data[5], PDO::PARAM_STR);
                str_replace('"',' ',$data);
                $import->execute();
                }
                $i++;

            }

编辑当前代码:

<?php
        if (isset($_POST['submit'])) {
            $i=0;
            require "connection.php";
            if (is_uploaded_file($_FILES['csv']['tmp_name'])) {
                echo "<h3>" . "File ". $_FILES['csv']['name'] ." uploaded successfully." . "</h3>";
            }
            //Import uploaded file to Database
            $handle = fopen($_FILES['csv']['tmp_name'], "r");
            $import=$db->prepare("INSERT INTO adherence(
                    dateandtime,
                    lastname,
                    firstname,
                    paidtime,
                    approvedtime,
                    notadhering)VALUES(
                    ?,?,?,?,?,?)");
            $badlines=[];
while (($data = fgetcsv($handle, 1000,"\t",'"')) !== FALSE) {
    if($i>0) {
        //skip but keep track of bad data
        if(count($data) !== 5){
            $badlines[] = $i;
            continue;
        }
        $myDate =  date("Y/m/d",strtotime(str_replace('/','-',$data[0])));
        $names = explode(',', $data[1]);
        $import->bindParam(1, $myDate, PDO::PARAM_STR);
        $import->bindParam(2, $names[0], PDO::PARAM_STR);
        $import->bindParam(3, $names[1], PDO::PARAM_STR);
        $import->bindParam(4, $data[2], PDO::PARAM_STR);
        $import->bindParam(5, $data[3], PDO::PARAM_STR);
        $import->bindParam(6, $data[4], PDO::PARAM_STR);
        $import->execute();
    }
    $i++;

}
            $removal=$db->prepare("delete FROM adherence WHERE approvedtime = '0' OR notadhering IS NULL");
            $removal->execute();

            fclose($handle);
        }

1 个答案:

答案 0 :(得分:0)

"name, lastname"是一列,因为它被双引号括起来。 您的代码最初通过将单引号作为enclosure参数传递错误配置为fgetscsv,导致它忽略这些双引号并将单列视为包含逗号的两列。

由于您的新文件是制表符分隔符,因此不会在逗号上分割。

要修复,请将正确的enclosure参数传递给fgetcsv,然后自行拆分列:

$badlines=[];
while (($data = fgetcsv($handle, 1000,"\t",'"')) !== FALSE) {
    if($i>0) {
        //skip but keep track of bad data
        if(count($data) !== 5){
            $badlines[] = $i;
            continue;
        }
        $myDate =  date("Y/m/d",strtotime(str_replace('/','-',$data[0])));
        $names = explode(',', $data[1]);
        $import->bindParam(1, $myDate, PDO::PARAM_STR);
        $import->bindParam(2, $names[0], PDO::PARAM_STR);
        $import->bindParam(3, $names[1], PDO::PARAM_STR);
        $import->bindParam(4, $data[2], PDO::PARAM_STR);
        $import->bindParam(5, $data[3], PDO::PARAM_STR);
        $import->bindParam(6, $data[4], PDO::PARAM_STR);
        $import->execute();
    }
    $i++;

}