简体   繁体   中英

Import CSV file into database

I'm using following function to import the csv file into db. All is working fine. But I'm also want its also insert $_SESSION['userid'] with CSV file. please help me. thanks

    function csv_file_to_mysql_table($source_file, $target_table, $max_line_length=10000) { 
            if (($handle = fopen("$source_file", "r")) !== FALSE) { 
                $columns = fgetcsv($handle, $max_line_length, ","); 
                foreach ($columns as &$column) { 
                    $column = str_replace(".","",$column); 
                } 
                $insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).")\nVALUES"; 
                while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) { 
                    while (count($data)<count($columns)) 
                        array_push($data, NULL); 
                    $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; 
                    mysql_query($query); 
                } 
                fclose($handle); 
            } 
        } 

        function quote_all_array($values) { 
            foreach ($values as $key=>$value) 
                if (is_array($value)) 
                    $values[$key] = quote_all_array($value); 
                else 
                    $values[$key] = quote_all($value); 
            return $values; 
        } 

        function quote_all($value) { 
            if (is_null($value)) 
                return "NULL"; 

            $value = "'" . mysql_real_escape_string($value) . "'"; 
            return $value; 
        }
        csv_file_to_mysql_table($uploadfile,"import_csv");
        echo "file is imported successfully!";
    }

I imagine something like this would work. Just append the extra field after you do your array joins.

I don't know what your user_id column is called, i just assumed user_id

$insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).",user_id)\nVALUES";

add the user_id value to the values argument

$query = "$insert_query_prefix (".join(",",quote_all_array($data)).",".$_SESSION['userid'].");";

Look for "ADD HERE"... just tack the column name in after you get them and also add the data to the end of your array once its built... should be simple as that.

            foreach ($columns as &$column) { 
                $column = str_replace(".","",$column); 
            }
//ADD HERE
$columns[] = "userid";
            $insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).")\nVALUES"; 
            while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) { 
                while (count($data)<count($columns)) 
                    array_push($data, NULL); 
//ADD HERE
$data[] = $_SESSION['userid']

                $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; 
                mysql_query($query); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM