将单个CSV文件导入MySQL中的多个表

时间:2017-07-29 07:05:58

标签: php mysql csv import

我想在MySQL中将包含7列的单个CSV导入2个表。

第1列,第2列和第3列进入table1中的单行。第4列和第5列在table2中作为一行。第6列和第7列在同一个表2中再次成为一行。

如何直接使用PHP或mysql来完成?

2 个答案:

答案 0 :(得分:0)

首先从csv中获取所有值并将其存储在数组中。然后根据您的要求维护您的数组,然后开始循环和插入。

答案 1 :(得分:0)

<?php
        $efected = 0;
        $file_temp = $_FILES["file"]["tmp_name"];
        
        $handle = fopen($file_temp, "r"); // opening CSV file for reading
        
        if ($handle) { // if file successfully opened
            $i=0;
            while (($CSVrecord = fgets($handle, 4096)) !== false) { // iterating through each line of our CSV
            
                if($i>0)
                {
                    
                    list($name, $gender, $website, $category, $email, $subcat) = explode(',', $CSVrecord); // exploding CSV record (line) to the variables (fields)
                    //print_r($subcat); sub_cat_ids
                    
                    if($email!='')
                    {
                    
                        $chk_sql = "select * from employees where employee_email='".$email."'";
                        $chk_qry = mysqli_query($conn, $chk_sql);
                        $chk_num_row = mysqli_num_rows($chk_qry);
                        
                        $cat_array = array(trim($category));
                        $subcat_array = explode( ';', $subcat );
                        if(count($subcat_array)>0)
                        {
                            $subcat_array_new = array();
                            foreach($subcat_array as $key => $val)
                            {
                                $subcat_array_new[] = trim($val);
                            }
                        }
                        
                        $cat_sub_cat_merge = array_merge($cat_array, $subcat_array_new);
                        $cat_subcat_comma = implode( "','", $cat_sub_cat_merge );
                        
                        foreach($cat_array as $cat_key => $cat_val)
                        {
                            $chk_cat_sql = "select * from employee_cats where cats_name = '".$cat_val."'";
                            $chk_cat_qry = mysqli_query($conn, $chk_cat_sql);
                            $chk_cat_num_row = mysqli_num_rows($chk_cat_qry);
                            //$all_cat_array = array();
                            if($chk_cat_num_row == 0)
                            {
                                $new_cat_ins = "insert into employee_cats set cats_name = '".$cat_val."', parent_cat_id = '0' ";
                                $new_cat_ins_qry = mysqli_query($conn, $new_cat_ins);
                            }
                        }
                        
                        foreach($subcat_array_new as $subcat_key => $subcat_val)
                        {
                            $chk_subcat_sql = "select * from employee_cats where cats_name = '".$subcat_val."'";
                            $chk_subcat_qry = mysqli_query($conn, $chk_subcat_sql);
                            $chk_subcat_num_row = mysqli_num_rows($chk_subcat_qry);
                            //$all_cat_array = array();
                            if($chk_subcat_num_row == 0 && trim($subcat_val)!='')
                            {
                                //$category
                                $get_catid_sql = "select * from employee_cats where cats_name = '".trim($category)."'";
                                $chk_catid_qry = mysqli_query($conn, $get_catid_sql);
                                $fetch_cat_info = mysqli_fetch_array($chk_catid_qry);
                                $fetch_cat_id = $fetch_cat_info['cats_id'];
                                $new_subcat_ins = "insert into employee_cats set cats_name = '".$subcat_val."', parent_cat_id = '".$fetch_cat_id."' ";
                                $new_subcat_ins_qry = mysqli_query($conn, $new_subcat_ins);
                            }
                        }
                        
                        
                        $get_cat_sql = "select * from employee_cats where cats_name in ('".$cat_subcat_comma."')";
                        $get_cat_qry = mysqli_query($conn, $get_cat_sql);
                        $get_cat_num_row = mysqli_num_rows($get_cat_qry);
                        
                        $sub_category_ids = array();
                        if($get_cat_num_row>0)
                        {
                            while($fetch_cat_id = mysqli_fetch_array($get_cat_qry))
                            {
                                if($fetch_cat_id['parent_cat_id']==0)
                                {
                                    $category_id = $fetch_cat_id['cats_id'];
                                }
                                else
                                {
                                    $sub_category_ids[] = $fetch_cat_id['cats_id'];
                                }
                            }
                            
                            $sub_cat_id_vals_comma = implode(",", $sub_category_ids);
                        }
                        else
                        {
                            $category_id = 0;
                            $sub_cat_id_vals_comma = "";
                        }
                        
                        if($chk_num_row>0)
                        {
                            // and here you can easily compose SQL queries and map you data to the tables you need using simple variables
                            $update_sql = "update employees set 
                                                            employee_name='".$name."', 
                                                            employee_gender='".$gender."', 
                                                            employees_website='".$website."',
                                                            employees_cat_id='".$category_id."',
                                                            sub_cat_ids='".$sub_cat_id_vals_comma."'
                                                            where employee_email='".$email."'";
                            $mysqli_qry = mysqli_query($conn, $update_sql);
                            
                        }
                        else
                        {
                        
                            // and here you can easily compose SQL queries and map you data to the tables you need using simple variables
                            $insert_sql = "insert into employees set 
                                                                    employee_name='".$name."', 
                                                                    employee_gender='".$gender."', 
                                                                    employees_website='".$website."',
                                                                    employees_cat_id='".$category_id."',
                                                                    sub_cat_ids='".$sub_cat_id_vals_comma."',
                                                                    employee_email='".$email."'";
                            $mysqli_qry = mysqli_query($conn, $insert_sql);
                        }
                        
                        $efected = 1;
                    }
                }
            
            $i++;
            }
            fclose($handle); // closing file handler
        }
    
    if($efected==1)
    {
        header('location:'.$site_url.'?import=success');
    }
    else
    {
        header('location:'.$site_url.'?import=failed');
    }

?>