gpt4 book ai didi

php - 从 excel 文件中读取第一行并创建包含 column1 字段的 mysql 表

转载 作者:行者123 更新时间:2023-11-29 03:02:35 26 4
gpt4 key购买 nike

1) 上传excel文件的输入选项。

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

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

我在这里尝试了下面的代码,我可以读取 excel 文件数据并将其导入表格中。但它仅在数据库中已创建数据库表时才起作用。我希望根据 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 文件时都必须创建一个表。我不认为创建很多表是个好主意,因为1.难以维护。2.不同上传的数据没有关联。(所以将所有数据移动到数据库中有什么意义?)3.当你必须获取一些数据时,你能记住要搜索哪个表吗?

以下是我关注的一些解决方案:1.如果要过滤数据,可以使用同一张表并添加一个标志(例如上传的时间戳)。2.如果列名不时变化,您可以简单地创建一个包含列(id,column1,column2 ...)的表并每次都保存列名。3.如果你只是想保存和下载excel数据,不想用你的程序搜索或排序,你可以简单地将文件保存在一个文件夹中,并将文件夹路径保存在数据库中。

如果您真的非常想在每次读取文件时创建表格,您可以尝试以下方法:

假设您的 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);

关于php - 从 excel 文件中读取第一行并创建包含 column1 字段的 mysql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20851711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com