使用动态表头从文件导入csv

时间:2019-04-24 08:58:37

标签: 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";
        }
            }
    }
}

}

1 个答案:

答案 0 :(得分:0)

快速浏览一下您的代码,我发现有些不对劲:

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循环来读取数据,因此认为会有更多记录。