Web input CSV to DB

Soldato
Joined
5 Dec 2003
Posts
2,716
Location
Glasgow
Hi,

I am trying to allow users to enter CSV text or file upload via a web-form which I will then add to a database table using PHP. Can anyone advise the best way to do this?

I think I have a good tutorial to follow for uploading a file using jQuery. I have done some AJAX and get/post forms but not sure how I would do with a big long CSV string?

Also, is it easy enough to parse the CSV in PHP or is there a plugin worth getting? It's not a complicated CSV, just key-value pairs really.
 
Associate
Joined
18 Sep 2003
Posts
903
There are PHP libraries you can get for reading and writing not just CSV files but also Excel files. Allowing people to upload as an Excel file is more user friendly.

But from a programming aspect, it's much easier to stick to CSV and to use the built in fgetcsv function. This will read in a line of your CSV at a time and turn it into an array. You can just keep calling it and inserting the data until it returns false, and then that means you've reached the end of the file.

From the front end side of things, jQuery makes AJAX pretty easy and there are tutorials out there for it. I'm not sure what you mean by "a big long CSV string". Do you mean you are going to have a text area and let people paste data into it in a CSV format for upload? If so, you can just get the value of the text area with jQuery just as you would any other field, then include it in your AJAX request. But if you want to make it easier on yourself, forget the AJAX if you can and just make it a normal form that submits to your PHP script.
 
Soldato
OP
Joined
5 Dec 2003
Posts
2,716
Location
Glasgow
Yeah it will just be a textarea control. I just am used to doing forms where it would be field and then value so I wasn't sure how POSTing something with lots of commas etc. would go through but I guess it will be okay.

Thanks!
 
Soldato
Joined
3 Jun 2005
Posts
3,119
Location
The South
There's a good chance you'll hit the POST character limit, which granted you can change but isn't ideal. You'd be better off allowing the user to upload a CSV file and then do the processing on that.
 
Soldato
OP
Joined
5 Dec 2003
Posts
2,716
Location
Glasgow
Yeah I will have a file upload option as well.

I have tried implementing this today, I didn't use fgetcsv as it wanted a filehandle. But if I used another str_getcsv. So it picks up the commas fine but not new lines, I am not sure how these are sent in the POST? I didn't have Firebug or anything to take a look at. So I tried using explode("\r\n", $POST... but didn't seem to catch them either.
 
Associate
Joined
18 Sep 2003
Posts
903
Browsers should always send text as carriage return line feed, regardless of platform, so explode("\r\n",$_POST['your_field_name']) should have worked on the text area POST, assuming there are no line breaks within the data. With file uploads and data gathered with JavaScript however, you do need to worry about line endings.

If you can't get it to work, maybe you should post your code.
 
Back
Top Bottom