从Excel文件中读取第一行,并使用column1字段创buildmysql表

1)input选项上传一个Excel文件。

2)读取excel文件并根据其第一行字段在mysql数据库tabel中自动创build表,我的意思是PHP脚本应该创build表。

3)将excel文件数据导入到数据库。

我试着在这里下面的代码,我可以读取Excel文件数据,并将其导入到表中。 但是只有当数据库表已经在数据库中创build时才能工作。 我正在寻找创build数据库表根据Excel文件的第一列/标题字段自动。 有任何想法吗。?

<?php //connect to the database $connect = mysql_connect("localhost","root",""); mysql_select_db("excel_database",$connect); //select the table // if ($_FILES[csv][size] > 0) { //get the csv file $file = $_FILES[csv][tmp_name]; $handle = fopen($file,"r"); //loop through the csv file and insert into database do { if ($data[0]) { mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES ( '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."' ) "); } } while ($data = fgetcsv($handle,1000,",","'")); // //redirect header('Location: import.php?success=1'); die; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Import a CSV File with PHP & MySQL</title> </head> <body> <?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your file: <br /> <input name="csv" type="file" id="csv" /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> 

我不确定为什么每次读取csv文件时都必须创build表格。 我认为创build大量表格并不是一个好主意,因为1.坚持不懈。 2.来自不同上传的数据是不相关的(所以将所有数据移动到数据库有什么意义?)3.当你得到一些数据时,你能记得要search哪个表吗?

以下是我所关心的一些解决scheme:1.如果要过滤数据,可以使用同一个表并添加一个标记(例如,上传的时间戳)。 2.IF列的名称不时,你可以只是简单地创build一个表(ID,列1,列2 …),并保存列名每次以及。 3.IF你只是想保存和下载的Excel数据,不想search或sorting与您的程序,只需简单地将文件保存在一个文件夹中,并将文件夹path保存在数据库中。

如果你真的想要在每次读取文件时创build表格,可以尝试下面的内容:

假设你的csv文件如下所示:

 table_name,column_name_1,column_name_2,...,column_name_n table_name,column_data_1,column_data_2,...,column_data_n table_name,column_data_1,column_data_2,...,column_data_n ... 

你可以做这样的事情:

 $table_name = ''; // get the table name $data = array(); // data array $column_name = array(); // get the column names $column_len = array(); //Decide the column length for the table when you create it do { if ($file_line[0]) { if(empty($table_name)){ $table_name = $file_line[0];// get the table name } $column_num = count($file_line);//the number of columns for($i = 1;$i<$column_num;$i++){ if(empty($column_name[$i])){ $column_name[$i] = $file_line[$i]; // get the column name array } //get the max length for each column if(empty($column_len[$i]) || count($file_line[$i]) > $column_len[$i]){ $column_len = count($file_line[$i]); } $data_line[$i] = $file_line[$i]; } } } while ($file_line = fgetcsv($handle,1000,",","'")); //create the table $create_query = "CREATE TABLE {$table_name} IF NOT EXISTS(\n"; $create_query .= " id INT NOT NULL AUTO_INCREMENT ,\n" foreach ($column_name as $key => $name){ $create_query .= " {$name} VARCHAR({$column_len[$key]}) ,\n" } $create_query .= " PRIMARY KEY (id)\n);"; // Output the create_query should shows something like this: // CREATE TABLE table_name IF NOT EXISTS( // id INT NOT NULL AUTO_INCREMENT , // column_name_1 VARCHAR(1000), // column_name_2 VARCHAR(500), // ... // PRIMARY KEY (id) // ); mysql_query($create_query); //insert data(you have already worked this out so you can just ignore this part) $insert_query = "INSERT INTO {$table_name} VALUES \n"; $insert_array = array(); foreach($data as $line){ $line_str = implode(',',$line); $insert_array []= "(NULL,{$line_str})"; } $insert_query .= implode(",\n",$insert_array); $insert_query .= ";"; mysql_query($insert_query);