CSV 2 PHP 2 MySQL Insert
I wrote something last night that will save a lot of people a lot of time. It's a class that opens a .csv file (normally saved through Excel) and parses your data accordingly to build an INSERT query for it. Then it inserts all of the information into whatever table you specified. You can also choose to create the table if it doesn't exist. Below are the parameters included in the class.
$csv->csvFile('file.csv'); // The path to our file (file.csv)
$csv->mysqlTable('tablename'); // The table we are dealing with
$csv->makeTable(false); // Do we want to make the table if its not created, you must declare the proper params in the .csv (true/false)
$csv->testMode(false); // If we want to test or just go in head first (true/false)
$csv->response(); // this returns true if the executed query worked. You can use an if statement to check this, followed by the error/success msg in the next param..
$csv->responseText(); // the error/success msg
In a few days I will have a video tutorial online explaining how to get things setup.
If you're a picture person, below is an illustration of what it would be doing.

4 Sexy Responses to CSV 2 PHP 2 MySQL Insert
@chuck: It might be line 39 in test.php
$csv->csvFile($fileName);
Change that to read
$csv->csvFile($uploadFolder.$fileName);
Let me just say that this is an awesome app. Just what i needed! But there are a couple of things I was wondering about. Does the class strip zeros from numbers? One of my fields has numbers like ‘0005′, but when i import, they turn into ‘5′. How can I remove this? And another thing. How could i do a DELETE FROM sometable WHERE someid = ‘0005′ BEFORE the INSERTion of the rows in the end? I tried doing it in the top of test.php, but then the added rows get deleted as well.
Thanks!
@Olaf: You cannot delete using this class. I’m not sure why it’s making your number into 5. Try changing the data type to VARCHAR.
Feel free to put in your 2 cents. I need cash.
Code must be put in between <code> </code> tags or there will be no soup for you.




had this to say...
for some reason it bombs if i change the file name. and if i change the data in the csv file it still spits out the same information over and over again rob, tim,… kim..
no matter what data is in there