简体   繁体   English

我如何获取Excel中每一列的值并插入MySQL

[英]How can i get the value of each column in excel and insert to MySQL

I am newbie in SQL and Excel is anyone can guide me for this? 我是SQL和Excel的新手,有人可以为此指导我吗?

This is my full script. 这是我的完整剧本。

 <?php include 'function/functions.php'; $excel = $_POST['files']; $upload = $_POST['upload']; $query = mysql_query("INSERT INTO clients (NAME, AGE, SEX, ADDRESS, CONTACT_NUMBER) VALUES ($excel)"); if(isset($upload)) { $query; } ?> <!doctype HTML> <html> <head> </head> <body> <form action="" method="POST" /> Upload Excel File<br /> <input type="file" name="files" /><br /> <input type="submit" value="upload" name="upload" /> </form> </body> </html> 

i'll make simple code to make easy to understand for me,(anyway sorry for my bad english) 我将编写简单的代码来使我更容易理解(无论如何,我的英语不好)

my question is, How i can insert each corresponding column in excel in MySQL here's the picture. 我的问题是,如何在MySQL中的excel中插入每个对应的列,如下图。

enter image description here 在此处输入图片说明

enter image description here 在此处输入图片说明

Use fgetcsv to get contents from excel file as array values.Hope it works for You. 使用fgetcsv从Excel文件中获取内容作为数组值,希望它对您有用。

 if($_FILES["file"]["size"] > 0)
     {

      $file = fopen($filename, "r");
             while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
             {
                $sql = "INSERT into import(NAME,AGE,SEX,ADDRESS,CONTACT_NUMBER) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]')";
                mysql_query($sql);
             }
             fclose($file);
             echo "CSV File has been successfully Imported";
     }
     else
     echo "Invalid File:Please Upload CSV File";

You probably need some kind of Excel interpreter. 您可能需要某种Excel解释器。 I don't believe PHP comes with a default one. 我不相信PHP带有默认值。

This question seems to have been answered here: Reading an Excel file in PHP 这个问题似乎已经在这里得到回答: 用PHP读取Excel文件

I use PHP-ExcelReader to read xls files, and works great. 我使用PHP-ExcelReader读取xls文件,效果很好。

It seems to be a standalone library that you can include. 它似乎是可以包含的独立库。

I would try that and then formulate a MySQL string to then insert into your database. 我会尝试这样做,然后制定一个MySQL字符串,然后插入到您的数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM