简体   繁体   中英

how do I import an excel data into an existing mysql table with php

I am working on a project, where the users can create a new contact group as well as be able to update existing ones with excel data.

creating a new contact group is not the problem, but inserting the content of the document into the database has been a major problem for me.

or which format can be imported easily into the database with php.

The data looks like this:

|name | |email | |telephone|

|me | |we@us.com| |080234444| etc

any advice, suggestion or direction will be appreciated.

MySql can easily import CSV* data from a file using the LOAD DATA INFILE command. If you need to parse other excel data, you will need extra methods and logic to read the file and manually populate the database.

You can read more info on LOAD DATA INFILE

*Essentially any delimited text (TSV etc)

Please note there are many limitations/gothchas with delimited data, for example, the inability to hold unescaped commas in csv text. Be sure you source file adheres and respects CSV rules or else you may end up with corrupt data.

EDIT

Now that you have provided some example of your data you may want to try this (or something similar):

LOAD DATA INFILE `your filename` INTO `Table` 
Fields Terminated BY '|' 
Lines Terminated BY '\r\n'
IGNORE 1 LINES 
(@Col1, @Col2, @Col3, @Col4, @Col5)
SET Name = TRIM(@Col1), Email = TRIM(@Col3), Telephone = TRIM(@Col5)

Here, you load the data into temporary variables, which then get instered into the table. Note the file must exist on the local machine.

EDIT

Try this fixed SQL

LOAD DATA INFILE 'your filename' INTO TABLE `Table` 
Fields Terminated BY '|' 
Lines Terminated BY '\r\n'
IGNORE 1 LINES 
(@Col1, @Col2, @Col3, @Col4, @Col5)
SET Name = TRIM(@Col1), Email = TRIM(@Col3), Telephone = TRIM(@Col5)

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