[英]upload csv and parse content to mysql
I would like to upload a csv file and than to show what for columns has. 我想上传一个csv文件,而不是显示列的内容。 Than I would like to assign to this columns a mysql table and columns of it to upload content to database.
比我想给这列分配一个mysql表,并把它的列上载到数据库。
**CSV -> Upload (HTML Form)**
**show CSV columns like**
| select table and show columns in select combo
-----------------------------------------------------------
id | dropdown with mysql tabel columns
-----------------------------------------------------------
name | dropdown with mysql tabel columns
-----------------------------------------------------------
address | dropdown with mysql tabel columns
UPLOAD to mysql 上载到mysql
Having not to many experience I would like to know: Must the CSV be uploaded first to the server or I can handle on the flow? 经验不足,我想知道:CSV必须先上传到服务器,还是可以处理?
Here is the code for it: 这是它的代码:
$host="hostname"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name = "database_name"; // Database name
$tbl_name = "table_name"; // Table name
// Connect to server and select database.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if ($_FILES[file][size] > 0) {
//get the csv file
$file = $_FILES[file][tmp_name];
$handle = fopen($file,"r");
$count = 0;
//loop through the csv file and insert into database
//this loop ignores the first row of CSV file as they are the headers
$flag = true;
do {
if ($data[0]) {
$count = $count + 1;
if($flag)
{
$flag=false;
continue;
}
else
{
mysql_query("INSERT INTO $tbl_name (email,password) VALUES
(
'".addslashes($data[0])."', //CSV equivalent of id
'".addslashes($data[1])."', //CSV equivalent of name
'".addslashes($data[2])."', //CSV equivalent of address
)
");
}
}
} while ($data = fgetcsv($handle,10000,",","'"));
Do upvote and mark as answer if this helps you. 如果有帮助,请投票并标记为答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.