Jump to content

Importing .csv file via phpMyAdmin


gwh

Recommended Posts

Hi everyone,

 

I'm trying to import a .csv file via phpMyAdmin. I started out in excel and then saved as .csv. The following are the headings in the excel file which correspond to a table in phpMyAdmin:

 

shirtID, shirtTitle, shirtSKU, shirtDescLadies, shirtDescMen, shirtPrice, shirtColours, shirtSizes, shirtImage, shirtSwatch

 

The first field, shirtID is an auto-incrementing primary key. All fields are set to be Not Null with the exception of three fields, ie. shirtDescLadies, shirtDescMen and shirtSwatch which are set to NULL.

 

I've inserted five rows of data under each column heading and then saved the file as a .csv file. I then opened up the file in a text editor and deleted the first row which contain the headings.

 

Back in phpMyAdmin, I browsed for the .csv file and have the following settings:

 

Format of imported file: CSV

Replace table data with file: unchecked

Ignore duplicate rows: unchecked

Fields terminated by: ,

Fields enclosed by: "

Fields escaped by: \

Lines terminated by: auto

 

Then I typed in the column names in the "column names field". I typed all of them in except the first primary key column, ie. shirtID

 

When I click "Go", it only inserts the first row and gives me the following error:

 

Invalid field count in CSV input on line 2.

 

Out of the 5 sample rows that I inserted, all the fields were populated with data with the exception of the last column, shirtSwatch, which was left blank. Also one of the shirtDescMen fields was left blank, but since both these fields were set to NULL in the database table I didn't think this would matter.

 

Can anyone tell me what I'm doing wrong?

 

Appreciate any help.

 

Link to comment
Share on other sites

Thanks for the replies,

 

The following is the SQL for the table:

 

CREATE TABLE `shirts` (

  `shirtID` int(10) unsigned NOT NULL auto_increment,

  `shirtTitle` varchar(100) NOT NULL,

  `shirtSKU` varchar(255) NOT NULL,

  `shirtDescLadies` varchar(255) default NULL,

  `shirtDescMen` varchar(255) default NULL,

  `shirtPrice` double NOT NULL,

  `shirtColours` varchar(255) NOT NULL,

  `shirtSizes` varchar(100) NOT NULL,

  `shirtImage` varchar(255) NOT NULL,

  `shirtSwatch` varchar(255) default NULL,

  PRIMARY KEY  (`shirtID`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

 

I've attached a screenshot of what it looks like in Excel and below is what it looks like when I open up the .csv file in a text editor, although as mentioned I remove the first row before I try to import it:

 

shirtID,shirtTitle,shirtSKU,shirtDescLadies,shirtDescMen,shirtPrice,shirtColours,shirtSizes,shirtImage,shirtSwatch

,Essentials _ Ladies and Men's Coordinates,1000,"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.","3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.",40,"Ink, Burgundy, Blue, White, Charcoal ",6-26,1000.jpg,

,Essentials _ Ladies and Men's Coordinates,2000,"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.",NULL,40,"Ink, Burgundy, Blue, White, Charcoal ",6-26,2000.jpg,

,Essentials _ Ladies and Men's Coordinates,3000,"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.","3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.",40,"Teal, Blue, Lilac ",SM-4XL,3000.jpg,

,Essentials _ Ladies and Men's Coordinates,4000,"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.","3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.",40,"Sky, Wheat, Blue, Navy ",6-26,4000.jpg,

,Essentials _ Ladies and Men's Coordinates,5000,"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.",NULL,40,White,6-26,5000.jpg,

 

[attachment deleted by admin]

Link to comment
Share on other sites

Looks like there are a couple problems.  First you dont need the first line in the csv with the headers. Second your not enclosing all your fields in quotes.

 

try formatting it something like

NULL,"Essentials _ Ladies and Men's Coordinates","1000","3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.","3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton.","40","Ink, Burgundy, Blue, White, Charcoal ","6-26","1000.jpg", ""

Link to comment
Share on other sites

OK, in Excel, format the columns correctly so they are of the correct type for the data that they contain. You can see that your description fields contain commas within the text so that is obviously going to cause an issue if you are using a comma as a field delimiter. Export the data from Excel or Access but use a pipe as a delimiter | for fields.

Link to comment
Share on other sites

OK, in Excel, format the columns correctly so they are of the correct type for the data that they contain. You can see that your description fields contain commas within the text so that is obviously going to cause an issue if you are using a comma as a field delimiter. Export the data from Excel or Access but use a pipe as a delimiter | for fields.

 

Only problem is that when I save as a .csv file from excel, it doesn't give you the option to choose a delimiter. Or am I missing something?

Link to comment
Share on other sites

Ok so I opened up the excel file in access and saved it as a .csv file with a pipe delimiter. The code it produced looks like this:

 

"Essentials ? Ladies and Men's Coordinates"|"1000"|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|40.00|"Ink, Burgundy, Blue, White, Charcoal "|"6-26"|"1000.jpg"|

|"Essentials ? Ladies and Men's Coordinates"|"2000"|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|"NULL"|40.00|"Ink, Burgundy, Blue, White, Charcoal "|"6-26"|"2000.jpg"|

|"Essentials ? Ladies and Men's Coordinates"|"3000"|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|40.00|"Teal, Blue, Lilac "|"SM-4XL"|"3000.jpg"|

|"Essentials ? Ladies and Men's Coordinates"|"4000"|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|40.00|"Sky, Wheat, Blue, Navy "|"6-26"|"4000.jpg"|

|"Essentials ? Ladies and Men's Coordinates"|"5000"|"3/4 Sleeve with Roll Up Feature, 65% Polyester, 35% Cotton."|"NULL"|40.00|"White"|"6-26"|"5000.jpg"|

 

But this still hasn't solved the problem. It's still only inserting the first record and then throws up the same error:

 

Invalid field count in CSV input on line 2

 

Any suggestions?

Link to comment
Share on other sites

That worked – thanks. The only problem is that if I add two inverted commas, ie "" to the empty columns in the excel spreadsheet and then export to .csv in access, it adds another set of inverted commas around the two existing ones, eg. """"

 

Is there anyway around this?

 

Also, I noticed that access is stripping out my dashes and replacing them with a question mark, eg.

 

Essentials ? Ladies and Men's Coordinates

 

Do you know why this is happening?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.