title: How do I import a csv with or without headers and allow records to have empty column fields?
body: <p>I looked at other question and answers already.  That's how I got the base codes I'm working with.  I modified it to my needs.  Basically I'm working with 2 tables.  First table creates one record, then second table is all the records of the csv file and it copies the id that was just created in table 1 into the job_id field of table 2.</p>
  
  <p>The thing that doesn't work is if any of the csv rows are not completely filled in with all the columns.  the code just inserts into the database the rows that are all filled in and when it hits the first row that has blank columns, it stops running.  neither of the 2 versions below works.</p>
  
  <p>Here is version one if the csv doesn't have headers.</p>
  
  <pre><code>if ($_FILES[csv][size] &gt; 0) {
  
      include_once('db.php');
      //get the csv file
      $file = $_FILES[csv][tmp_name]; 
      $handle = fopen($file,"r");
  
      $job_name = $_POST['job_name'];
      $file_name = $_FILES[csv][name];
      $date_created = $_POST['date_created'];
  
      $sql = "INSERT INTO jobs (job_name, file_name, date_created) VALUES ('$job_name','$file_name','$date_created')";
      mysql_query($sql) or die(mysql_error());
  
      $job_id = mysql_insert_id();
  
      //loop through the csv file and insert into database
      do {
          if ($data[0]) {
              mysql_query("INSERT INTO people (job_id, company, first, last, address1, address2, city, state, zip, phone, email, tracking_number, year, make, model, image, misc1, misc2, misc3, misc4, misc5, misc6, list) VALUES ( '".$job_id."', '".addslashes($data[0])."', '".addslashes($data[1])."', '".addslashes($data[2])."', '".addslashes($data[3])."', '".addslashes($data[4])."', '".addslashes($data[5])."', '".addslashes($data[6])."', '".addslashes($data[7])."', '".addslashes($data[8])."', '".addslashes($data[9])."', '".addslashes($data[10])."', '".addslashes($data[11])."', '".addslashes($data[12])."', '".addslashes($data[13])."', '".addslashes($data[14])."', '".addslashes($data[15])."', '".addslashes($data[16])."', '".addslashes($data[17])."', '".addslashes($data[18])."', '".addslashes($data[19])."', '".addslashes($data[20])."', '".addslashes($data[21])."') ");
          }
      }
      while ($data = fgetcsv($handle,1000,",","'"));
  
      //redirect
      header('Location: upload.php?success=1&amp;job_name='.$job_name.'&amp;file_name='.$file_name.'&amp;date_created='.$date_created.'&amp;job_id='.$job_id.'');
  
      die;
  
  }
  </code></pre>
  
  <p>Here is version two if the csv has a headers line at the top.</p>
  
  <pre><code>if ($_FILES[csv][size] &gt; 0) {
  
      include_once('db.php');
      //get the csv file
      $file = $_FILES[csv][tmp_name]; 
      $handle = fopen($file,"r");
  
      // get headers
      $columns = fgetcsv($handle,1000,",","'");
      $column_list = implode(",",$columns);
      $column_list = "job_id,".$column_list;
  
      $job_name = $_POST['job_name'];
      $file_name = $_FILES[csv][name];
      $date_created = $_POST['date_created'];
  
      $sql = "INSERT INTO jobs (job_name, file_name, date_created) VALUES ('$job_name','$file_name','$date_created')";
      mysql_query($sql) or die(mysql_error());
  
      $job_id = mysql_insert_id();
  
      //loop through the csv file and insert into database
      while ($data = fgetcsv($handle,1000,",","'")) {
          if ($data[0]) {
              for ($i=0;$i&lt;count($columns);$i++){
                  $data[$i]="'".mysql_real_escape_string($data[$i])."'";
              }
              $values = implode(",",$data);
              $values = "'".$job_id."',".$values;
              mysql_query('INSERT INTO people ('.$column_list.') VALUES ('.$values.')');
          }
      }
  
      //redirect
      header('Location: upload.php?success=1&amp;job_name='.$job_name.'&amp;file_name='.$file_name.'&amp;date_created='.$date_created.'&amp;job_id='.$job_id.'&amp;column_list='.$column_list.'&amp;values='.$values.'');
  
      die;
  
  }
  </code></pre>
  
  <p>Update:</p>
  
  <p>still didn't work with that answer.</p>
  
  <p>here is csv with headers line.  only the first row after the headers line is making it into the database still.  i need all the rows after headers line to make it into database.</p>
  
  <pre><code>company,first,last,address1,address2,city,state,zip,phone,email,tracking_number,year,make,model,image,misc1,misc2,misc3,misc4,misc5,misc6,list
  abc corp,john,doe,123 whatever st,suite 2,whatevercity,whateverstate,33333,1234567890,email@email.com,yes,2006,toyota,corolla,image.jpg,something,something,something,something,something,something,something
  ,Jim,Smith,address 1,,city 1,state 1,zip 1,,,,2007,honda,accord,,,,,,,,
  ,Joe,Tester,address 2,,city 2,state 2,zip 2,,,,2008,nissan,maxima,,,,,,,,
  </code></pre>