gwh Posted December 4, 2009 Share Posted December 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/ Share on other sites More sharing options...
JonnoTheDev Posted December 4, 2009 Share Posted December 4, 2009 Your data may have a , in it so the import thinks it is the start of a new field Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971275 Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 Can you provide you mysql table layout and a sample from the csv file? Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971294 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971314 Share on other sites More sharing options...
JonnoTheDev Posted December 4, 2009 Share Posted December 4, 2009 This is not csv format. Just look at it. There are a million commas. Where is the end of a record line? Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971321 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 That's how it came out when I saved the excel file as a .csv file. I don't know what the alternative is? Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971323 Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 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", "" Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971327 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 Yes but this file will eventually be hundreds of rows long. I can't really format it all manually. Is there another way to get excel to export a proper csv file? Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971330 Share on other sites More sharing options...
JonnoTheDev Posted December 4, 2009 Share Posted December 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971332 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971336 Share on other sites More sharing options...
JonnoTheDev Posted December 4, 2009 Share Posted December 4, 2009 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? Import the xls file into Access and then export from there. Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971339 Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 Control Panel -> Regional -> reginal options -> customize -> list seperator to change the comma to a pipe Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971340 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 Thanks I'll give that a try. Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971352 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971460 Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 You need something to hold the place of your autoincrement field and your missing your last field to so it should have "" there. See my first post about those fields. Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971473 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971483 Share on other sites More sharing options...
JustLikeIcarus Posted December 4, 2009 Share Posted December 4, 2009 Thats a weird one. Maybe something to do with the field type in access? As for the extra quotation marks... do you have a good text editor that you can just insert what you need? Or try just setting the value of the fields to space in access. Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971490 Share on other sites More sharing options...
gwh Posted December 4, 2009 Author Share Posted December 4, 2009 Will probably just experiment with using a space in access. Thanks for the suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/183981-importing-csv-file-via-phpmyadmin/#findComment-971494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.