doubei2231 2016-03-02 13:45
浏览 89

无法将数据从csv插入到具有未知列数的mysql数据库中

I'm having this very annoying problem for couple of days already, and I did research and everything, but it seems to be bigger than my programming skills and knowledge.

I have a situation where user can define custom columns in tables in my database, and now I need to import CSV files into that user's custom tables. Since I recently found out that tables need to be "dynamic" this is how I handled importing before:

$file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");
        $name = $_FILES['file']['name'];
    if (strrpos($name, '.csv', -4)) {//handling the csv file
                    do {
                        if ($data[0]) {
                            $mysqli->query("INSERT INTO `test` (`name`, `surname`, `email`) VALUES
                            (
                                '" . addslashes($data[0]) . "',
                                '" . addslashes($data[1]) . "',
                                '" . addslashes($data[2]) . "'
                            )
                        ");

                        }
                    } while ($data = fgetcsv($handle, 1000, ",", "'"));

And that worked perfectly with static tables. But since I dont't know how many columns table will have, I tried something like this:

Building query method

 if (strrpos($name, '.csv', -4)) {//ubacije vrijednosti csv fajla u bazu
                do {
                    if ($data[0]) {
                        $query = "INSERT INTO $tabela (";
                        $last = end($anotherArray);
                        foreach ($anotherArray as $something) {//$anotherArray contains names of fields in user's custom table
                            if ($something != $last) {
                                $query .= "" . $something . ",";
                            } else {
                                $query .= "" . $something . ")";
                            }
                        }

                        $query .= "VALUES (";
                        foreach($data as $val){
                       $query .= "'$val',";
}


    }while ($data = fgetcsv($handle, 1000, ",", "'"));

                $query=rtrim($query,',');
                $query.=")";
                //var_dump($query);
                $rez=$mysqli->query($query);
                //var_dump($rez);

But problem with this code is if csv file contains for example 2 or more colums like so:enter image description here

everything becomes part of that VALUES part of query. So query looks like this: "INSERT INTO tabela12312334 (user_facebook_id,ime,prezime) VALUES('123123123','Ognjen','Babic','123123123','Neven',Babic)" and of course number of fields isn't same as number of values.

Please help me to solve this, I'm desperate.

  • 写回答

1条回答 默认 最新

  • dongyuqi3808 2018-07-25 04:19
    关注

    Try this

    LOAD DATA LOCAL INFILE  
    'c:/path-to-csv-file/your-file.csv'
    INTO TABLE your_awesome_table  
    FIELDS TERMINATED BY ',' 
    ENCLOSED BY '"'
    LINES TERMINATED BY '
    '
    IGNORE 1 ROWS
    (field_1,field_2 , field_3);
    

    NB: the first row as the title for your fields, you might have to include a header in your CSV file

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示