i need the script that will reae the contents of the csv file & store it into the 2 tables in MySql DB
Main Topics
Browse All TopicsI am new at this & is looking for a csv file upload script using php. The file before being uploaded need to go through some validation (eg, file type & check the various column for integer & date type) before inserting into DB. once validation is done, the file will be inserted into 2 tables in DB.
Can someone please help?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
It will be able to read files on the same server - just give it the correct file path. It will also be able to read files on other servers, ie. using a URL as the path, but only if allow_url_fopen is On for that server (the default). See my comments here:
http://www.experts-exchang
4096 is the number of bytes to reads into memory for each line. This value should handle most situations - it's at least 2048 characters per line - so is commonly used. You can make this value smaller if your lines are short. I think you'd only bother if memory/performance of your website was critical.
My mistake, that line doesn't need to be there so get rid of it. It only applies if you are using SQL SELECT statements and it's in the wrong place anyway. I accidentally copied from another piece of code I had.
Are you getting any other error messages?
Are you sure it is opening your csv file correctly? If you change '@fopen' to 'fopen', you should see an error message on the screen or in your PHP error log if it's unable to open the CSV file. The '@' symbol is there to suppress error messages but at the coding stage, you probably want to see them. When the site is live, you would probably want to change it back to '@fopen' and put this at line 51:
else {
echo "Problem opening csv file";
}
If the file is opening correctly, have you changed the SQL to match your table structure? As a debugging step, insert this line at line 33 to make sure your SQL command is what you expect:
echo $sql;
In the development stage, you might want to put echo statements (or error_log statements) all through your code to see exactly where the problem is or use a PHP debugger like XDebug or ZendDebugger.
i am receiving the following error:
Resource id #13INSERT INTO `gipps_student_info` (SUBMITTER_ID, PROJECT_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (4, gipps, 200, 2002);Could not successfully run query (INSERT INTO `gipps_student_info` (SUBMITTER_ID, PROJECT_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (4, gipps, 200, 2002);) from DB: Unknown column 'gipps' in 'field list'
i dont need to add values in sequential order as in the DB table since i am mentioning which columns the values go under? any idea how to solve the problem above?
in my csv if I have row headers, how do i omit while inserting in DB
ERROR MSG:
Resource id #13INSERT INTO `gipps_student_info` (SUBMITTER_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (4, 200, 2002)Could not successfully run query (INSERT INTO `gipps_student_info` (SUBMITTER_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (4, 200, 2002)) from DB: Duplicate entry '2002' for key 2
looks like it is reading the first line over again
disregard the prev message
i get this error but data is also inserted in the DB:
Resource id #13INSERT INTO `gipps_student_info` (SUBMITTER_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (4, 200, 2002)1Data is invalidINSERT INTO `gipps_student_info` (SUBMITTER_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (4, 200, 2003)1Data is invalidINSERT INTO `gipps_student_info` (SUBMITTER_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (, , )Could not successfully run query (INSERT INTO `gipps_student_info` (SUBMITTER_ID, SCHOOL_NAME, STUDENT_CODE) VALUES (, , )) from DB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , )' at line 1
how can i solve this bit?
Of these columns, is there one that can't be left blank? That is, can you identify one column that you wouldn't want to insert the values into the database if it's not given?
For example, if the data is useless without a SCHOOL_NAME, you could wrap lines 21-31 of your code above with:
if($col1 != "") {
// put code here
}
Otherwise, you could wrap these lines in:
if(($col1 != "") && ($col2 != "") && ($col3 != "")) { // etc.
}
With all the values so it would need at least one value to be given before it inserted a row.
Lastly, lines 10-19 could be condensed into:
$values = array_map("trim", $values);
Business Accounts
Answer for Membership
by: segurahPosted on 2009-04-19 at 18:06:46ID: 24181375
More that a cvs uploades you probably need and ETL (extraction, transforming and loading) see at : http://en.wikipedia.org/wi ki/Extract ,_transfor m,_load