简体   繁体   中英

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?

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.

enter image description here

enter image description here

Use fgetcsv to get contents from excel file as array values.Hope it works for You.

 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. I don't believe PHP comes with a default one.

This question seems to have been answered here: Reading an Excel file in PHP

I use PHP-ExcelReader to read xls files, and works great.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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