php - 从具有动态表头的文件导入 csv

标签 php mysql csv

我在将 csv 脚本导入 mysql 时遇到问题: 从现在开始,我可以将 csv 中的数据放入 mysql 中,但只能严格进行标题排列,但我从 csv 导出的数据总是会更改表头,而我无法控制它。 我的 csv 文件与表格不同,例如:

Name Status Last_name email_address employee_address phone_number

我需要的只是获取列名称并将其放入表中的右列中。

谢谢。

if (isset($_POST["import"])) {
$fileName = $_FILES["file"]["tmp_name"];

if ($_FILES["file"]["size"] > 0) {

    $file = fopen($fileName, "r");
    $captabel = true;
    $flag = true;

    while (($column = fgetcsv($file, 1000, ",")) !== FALSE) {
    if($captabel) { $captabel = false; continue; }  
    $celuleNecesare = array(0);
    foreach ($celuleNecesare as $value) {
    if(trim($column[$value]) == "" || $column[$value] == NULL)
    $flag = false;
    }   


    if($flag) {
       $query = "INSERT into test (name ,employee_status, surname, email, address, phone)
               values ('" . $column[2] . "','" . $column[7] . "','" . $column[8] . "','" . $column[5] . "','" . $column[0] . "','0" . $column[3] . "')";
       $result = mysqli_query($conn, $query);

        if (! empty($result)) {
            $type = "success";
            header( 'refresh: 0; success.php' );

        } else {
            $type = "error";
            $message = "Problem in Importing CSV Data";
        }
            }
    }
}

}

最佳答案

快速浏览一下您的代码,我发现有些问题:

if (isset($_POST["import"])) 
{
    $fileName = $_FILES["file"]["tmp_name"];

    if ($_FILES["file"]["size"] > 0) 
    {
        $file = fopen($fileName, "r");
        $captabel = true;
        $flag = true;

        while (($column = fgetcsv($file, 1000, ",")) !== FALSE) 
        {
            if($captabel) 
            {
                $captabel = false; 
                continue;
            }

            $celuleNecesare = array(0);  <-- Is this being set externally to the code listed here.

            foreach ($celuleNecesare as $value) 
            {
                if(trim($column[$value]) == "" || $column[$value] == NULL)
                    $flag = false;
            }       


            if($flag) 
            {
                $query = "INSERT INTO test 
                            (name ,employee_status, surname, email, address, phone)
                          VALUES
                            ('" . $column[2] . "','" . $column[7] . "','" . $column[8] . "','" . $column[5] . "','" . $column[0] . "','0" . $column[3] . "')";
                $result = mysqli_query($conn, $query);

                if (! empty($result)) 
                {
                    $type = "success";
                    header( 'refresh: 0; success.php' );
                } 
                else 
                {
                    $type = "error";
                    $message = "Problem in Importing CSV Data";
                }
            }
        }
    }
}

您有行 $celuleNecesare = array(0); ,然后循环遍历它,但这本身没有任何作用。

您的代码应该与此类似:

if (isset($_POST["import"])) 
{
    $fileName = $_FILES["file"]["tmp_name"];

    if ($_FILES["file"]["size"] > 0) 
    {
        $file = fopen($fileName, "r");
        $captabel = true;
        $flag = true;

        // Get the first row as the header row.
        $headers = fgetcsv($file, 1000, ",");

        // Variables to be used.
        $counter = 0;
        $nameCol = 0;
        $statuscol = 0;
        $lastnameCol = 0;
        $emailAddressCol = 0;
        $employeeAddressCol = 0;
        $phoneNumberCol = 0;

        foreach($headers as $header)
        {
            // Name Status Last_name email_address employee_address phone_number

            // Lets work through the header row.
            switch($header)
            {
                case 'Name'     :   $nameCol = $counter;
                                    break;
                case 'Status'   :   $statusCol = $counter;
                                    break;
                case 'Last_name':   $lastNameCol = $counter;
                                    break;
                case 'email_address'    :   $emailAddressCol = $counter;
                                            break;
                case 'employee_address' :   $employeeAddressCol = $counter;
                                            break;
                case 'phone_number'     :   $phoneNumberCol = $counter;
                                            break;
                default     :   die("Unknown column, ".$header);
                                break;
            }

            $counter++;
        }

        while (($column = fgetcsv($file, 1000, ",")) !== FALSE) 
        {
            $query = "INSERT INTO test 
                        (name ,employee_status, surname, email, address, phone)
                      VALUES
                        ('" . $column[$nameCol]."','".$column[$employeeStatusCol]."','".$column[$lastnameCol]."','".$column[$emailAddressCol]."','".$column[$employeeAddressCol]."','0".$column[$phoneNumberCol]."')";
            $result = mysqli_query($conn, $query);

            // This section below would stop after the first record!!!!!
            if (! empty($result)) 
            {
                $type = "success";
                header( 'refresh: 0; success.php' );
            } 
            else 
            {
                $type = "error";
                $message = "Problem in Importing CSV Data";
            }
        }
    }
}

最后的 IF 条件意味着您只插入一条记录。不确定这是否是这个意思,但由于您有一个 while 循环来读取数据,所以认为会有更多记录。

关于php - 从具有动态表头的文件导入 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55826177/

相关文章:

php - 将jquery mobile和php mysql转为apk文件

mysql - 为什么与mysql交互时跳出while循环?

从外部 csv 文件导入时 phpmyadmin 转义反斜杠

php - FileMaker 安全问题

javascript - 除非刷新页面,否则 jQuery Ajax 点击不起作用

mysql - INSERT IGNORE 忽略时间的同时插入日期

python - 使用 pandas 或其他 python 模块读取特定列

java - 计算文件的总计和平均值

PHP MySQLi 准备好的语句 - 我应该包括所有字段吗?

javascript - Jquery Datepicker 卡在同一年