Jump to content

[SOLVED] Inserting CSV file data into a DB


ainoy31

Recommended Posts

Hello-

 

I have a csv file that gets uploaded and the data is suppose to insert into a DB.  I am abe to upload the csv file but it just hangs when trying to insert into the DB.  I have tried going over my code many times and can not figure it out.  Here is my code:

 

$target_dir = ".";

$target_path = $target_dir . $filename;

 

//check to see if the file already exist in the folder

if(!file_exists($target_path))

{

//moving the uploaded file to a new place

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target_path))

{

 

$handle = fopen($filename, "r");

 

//begin looping through the lines

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)

{

//setup the mysql query

$import="INSERT INTO carriers_schedule (s_ID, s_deptport, s_destport, s_startdate, carrier_email, carrier_name, carrier_phone) VALUES ('', '$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]', '$data[5]')";

 

 

mysql_query($import) or die(mysql_error());

}//end of WHILE

//close the file

fclose($handle);

 

Any suggestion would be much appreciated.  TY

 

Link to comment
https://forums.phpfreaks.com/topic/68868-solved-inserting-csv-file-data-into-a-db/
Share on other sites

What I usually do in this sort of thing is print out each time it inserts one, so I can see if it's working while it loads.

IE, add this:

print $import.'<br />';

above this line

mysql_query($import) or die(mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.