Welcome Guest, Not a member yet? Register   Sign In
CSV upload, once uploaded to a temp folder overwrite the existing database
#51

[eluser]the_unforgiven[/eluser]
Sorted it, realised something wasn't right with the CSV so checked it and insert 1st row to match the titles in the db and it worked....Weird
#52

[eluser]the_unforgiven[/eluser]
All working but now I need to get rid of the first line from the csv as these are titles so how can i incorp this into the code?
#53

[eluser]the_unforgiven[/eluser]
Again i'm using your code not the one i edited and still shows A Database Error Occurred

Error Number: 1136

Column count doesn't match value count at row 1

when i edit the csv and remove the 1st line which shows the titles like JobRef, Area, Parish, AbbrWorkType etc, so i need to know how to get this working with or without the headers and also the column count error
#54

[eluser]TheFuzzy0ne[/eluser]
You can just call:
Code:
fgetcsv($fh, 1000, ',');
to discard the first row.

As for your error. Please check the CSV for that row. There may be an extra column on the end if there's a space, which would explain the problem. You may need to add a little error checking to make sure each row has exactly the number of columns you expect it to have.
#55

[eluser]the_unforgiven[/eluser]
I have
Code:
fgetcsv($fh, 1000, ',');
already but it still insert the 1st row into the database which in this case is the titles.

I'm totally new to all this csv uploading as you probably guessed by now, so how would one check for errors etc?
#56

[eluser]TheFuzzy0ne[/eluser]
Make sure you're discarding the first row before you enter your loop.

As for error checking, so long as you know how many columns you're expecting, you can simply check each row using count() to make sure it has that number of columns. If it doesn't, it can show you an error that outputs the row, so you know where the problem is.
#57

[eluser]the_unforgiven[/eluser]
Right ok think i kinda got ya, before i do these error checking etc just wanted to show you what code i currently have:

Code:
$table = 'wc_program';
   $image_data = $this->upload->data();
      $fname = $image_data['file_name'];
      $fpath = $image_data['file_path'].$fname;
      $fh = fopen($fpath, "r+");
      $headers = 1;
  
   $insert_str = 'INSERT INTO wc_program (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES '."\n";

   if ($fh && $headers == 1) {
             // Create each set of values.
             while (($csv_row = fgetcsv($fh, 1000, ',')) !== false) {

                 foreach ($csv_row as &$row) {
                     $row = strtr($row, array("'" => "\\'", '"' => '\\"'));
                 }

                 $insert_str .= '("'
                     // Implode the array and fix pesky apostrophes.
                     .implode('","', $csv_row)
                     .'"),'."\n";
            
             }

             // Remove the trailing comma.
             $insert_str = rtrim($insert_str, ",\n");

             // Insert all of the values at once.
             $this->db->query($insert_str);
          
             $this->layout->buffer('content', 'program/success');
    $this->layout->render();
  
         }

So where would i put the strip headers thingy
#58

[eluser]TheFuzzy0ne[/eluser]
Think about it. All we're doing is pulling a row from the top of the CSV, and discarding it. Since your loop processes the rows, it would make sense to pull that first row before you get to the loop that does the processing.




Theme © iAndrew 2016 - Forum software by © MyBB