【问题标题】:Import CSV file with a column having date or text inside导入包含日期或文本列的 CSV 文件
【发布时间】:2015-04-24 07:14:34
【问题描述】:

我有一个 CSV 文件,其中有一列的日期格式为 d/m/Y 或单词“Illimité”在法语中的意思是无限。

我希望每次读取“Illimité”时都会将 NULL 值放入 M​​ySQL 表中。

这是我当前的 PHP 代码:

if (isset($_POST['submit'])) {
        $query = "TRUNCATE TABLE `formation` ";
        $result = mysql_query($query);
        $file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");
        while (($fileop = fgetcsv($handle, 1000, ";")) !== false) {

            $nom = $fileop[0];
            $prenom = $fileop[1];
            $formation = $fileop[16];
            $validite = $fileop[26];

            if (is_numeric($validite)) {
                $validite = date_format(date_create_from_format('d/m/Y', $validite), 'Y-m-d');
                $sql = mysql_query("INSERT INTO formation (nom,prenom,formation,validite,usermaj) VALUES ('$nom','$prenom','$formation','$validite','importCSV')");
                } else {
                $sql = mysql_query("INSERT INTO formation (nom,prenom,formation,validite,usermaj) VALUES ('$nom','$prenom','$formation',NULL,'importCSV')");
            }
        }

很遗憾,这不起作用。 MySql 没有显示任何错误,并且它一直为 NULL。 任何帮助我都将不胜感激。

【问题讨论】:

  • 将您的数据库设为 Charaset -UTF-8 编码
  • 所以你的 if 语句没有通过,你有没有试过 var_dump($validite),它是否给出数值以便它有机会通过?
  • @SunilPachlangia 我无法将其更改为 UTF-8,因为 MySQL 中的 type = date
  • 日期不是数字,所以它总是转到 else 语句
  • @JelledeFries 和 SunilPachlangia 你们都是对的,它不是数字!我的错。我需要找到如何测试它是否是 d/m/Y 格式的日期。

标签: php mysql csv import fgetcsv


【解决方案1】:

试试这个:

function myFunc($CSVValue){

    if(validateDate($CSVValue)){
        //your mysql logic where you put in the date
    }else{
        //your mysql logic where you put in the null
    }


}

function validateDate($date)
{
    $d = DateTime::createFromFormat('d/m/Y', $date);
    return $d && $d->format('d/m/Y') == $date;
}

函数是从 answerphp.net 复制而来的

-- 更新--

除了您提供的代码外,我不知道您的代码看起来如何。 如果你把它放在你的代码中,它可能看起来像这样:

if (isset($_POST['submit'])) {
        $query = "TRUNCATE TABLE `formation` ";
        $result = mysql_query($query);
        $file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");
        while (($fileop = fgetcsv($handle, 1000, ";")) !== false) {

            $nom = $fileop[0];
            $prenom = $fileop[1];
            $formation = $fileop[16];
            $validite = $fileop[26];

            $d = DateTime::createFromFormat('d/m/Y', $validite);

            if ($d && $d->format('d/m/Y') == $validite) {
                $validite = date_format(date_create_from_format('d/m/Y', $validite), 'Y-m-d');
                $sql = mysql_query("INSERT INTO formation (nom,prenom,formation,validite,usermaj) VALUES ('$nom','$prenom','$formation','$validite','importCSV')");
                } else {
                $sql = mysql_query("INSERT INTO formation (nom,prenom,formation,validite,usermaj) VALUES ('$nom','$prenom','$formation',NULL,'importCSV')");
            }
        }

【讨论】:

  • 我正在尝试您的代码,但它不起作用。我收到致命错误:无法重新声明 myFunc()
  • 你自己要努力,我的代码只是一个模板。你不应该只是复制它。
  • 非常感谢!它工作得很好!我不明白你的代码是如何工作的,我做错了。我还有很多东西要学。美好的一天!
  • @Geoffrey 很高兴我能帮上忙
猜你喜欢
  • 2019-07-01
  • 1970-01-01
  • 2015-02-08
  • 2018-08-06
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
相关资源
最近更新 更多