0

我想上传一个 csv 文件,而不是显示列的内容。比我想为这些列分配一个 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

上传到mysql

没有太多经验我想知道:必须先将 CSV 上传到服务器还是我可以处理流程?

4

1 回答 1

0

这是它的代码:

$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,",","'")); 

如果这对您有帮助,请投票并标记为答案。

于 2013-04-07T13:11:56.597 回答