-1

I want to insert some fields into a table in which part of the details will be supplied through a form by a user and others imported and av tried it out with the preceeding code but it combines all the fields into the first field of my table with a separated comma can anyone help me out.

if(is_uploaded_file($_FILES['file']['tmp_name'])){
   //Connect to the database
require("includefiles/myconnect.php");   
//Process the CSV file
   $handle = fopen($_FILES['file']['tmp_name'], "r");
   $data = fgetcsv($handle, 1000, ";"); 
   //Remove if CSV file does not have column headings
   while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
      $att0 = mysql_real_escape_string($data[0]);
      //echo $att0;
      $att1 = mysql_real_escape_string($data[1]);
      $att2 = mysql_real_escape_string($data[2]);
      $att3 = mysql_real_escape_string($data[3]);
      $att4 = mysql_real_escape_string($data[4]);
      $sql = "INSERT INTO `course_reg` (`coursecode`,`coursename`,`coursedescription`,`coursemaster`,`courselevel`)VALUES ('$att0','$att1','$att2','$att3','$att4')";
      mysql_query($sql) or die(mysql_error());
   }
  // mysql_close($link);
   echo "CSV file successfully imported.";
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • See this thread http://stackoverflow.com/questions/4143938/import-csv-file-directly-into-mysql – Vijay Aug 03 '11 at 09:08
  • possible duplicate of [Uploading large csv file into mysql database](http://stackoverflow.com/questions/6913547/uploading-large-csv-file-into-mysql-database) – Brad Larson Aug 18 '11 at 16:15

1 Answers1

0

Separated by a comma?

If a comma (i.e. ,) is indeed your separator, then why did you write this line:

$data = fgetcsv($handle, 1000, ";"); 

Change it into

$data = fgetcsv($handle, 1000, ",");

and you should be fine.

Pelle
  • 6,423
  • 4
  • 33
  • 50