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

[eluser]the_unforgiven[/eluser]
Database structure is as follows:

Code:
CREATE TABLE IF NOT EXISTS `wc_program_temp` (
  `JobRef` varchar(11) DEFAULT NULL,
  `Area` varchar(255) DEFAULT NULL,
  `Parish` varchar(255) DEFAULT NULL,
  `AbbrWorkType` varchar(255) DEFAULT NULL,
  `WorkType` varchar(255) DEFAULT NULL,
  `Timing` varchar(255) DEFAULT NULL,
  `TrafficManagement` varchar(255) DEFAULT NULL,
  `Location` varchar(120) DEFAULT NULL,
  `Duration` varchar(15) DEFAULT NULL,
  `Start` varchar(255) DEFAULT NULL,
  `Finish` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
#32

[eluser]TheFuzzy0ne[/eluser]
Dod gammit! Upon pasting the code into Netbeans, it became apparent where the problem was.

I had:
Code:
$insert_str = rtrim($insert_data, ','); // $insert_data?? What the...?

and it should've been:
Code:
$insert_str = rtrim($insert_str, ',');

Again, I've edited my code. Please try it again. If that doesn't work, I'll continue setting up my test environment.
#33

[eluser]the_unforgiven[/eluser]
Yes i saw that and changed to it
Code:
$insert_str = rtrim($insert_str, ',');
But still didnt work, let me try again on the code you edited.
#34

[eluser]TheFuzzy0ne[/eluser]
OK, not to worry. I'm setting this up to test it out myself. Smile
#35

[eluser]the_unforgiven[/eluser]
Still doesnt work Sad
#36

[eluser]TheFuzzy0ne[/eluser]
OK, I think I've figured this out. The problem? My stupidity...

rtrim() wasn't trimming the trailing comma, because it was followed by a newline. DOH!

This should finally work now. Sorry for making you wait for so long.

#37

[eluser]TheFuzzy0ne[/eluser]
And if you post back saying it doesn't work, I'm going to have to hurt you. Smile
#38

[eluser]the_unforgiven[/eluser]
Oh shit! I best emigrate then! Cos guess what.... It still don't work Sad Sorry for putting you through this pain, and I really appreciate your help! Smile
#39

[eluser]the_unforgiven[/eluser]
This
Code:
//Get the data from some web resource in csv format
     $data = file_get_contents("http://localhost/sussex/assets/uploads/wc_program.csv");
  
   //Explode the csv into an array of lines
   $lines = explode(",", $data);
    
   $file_handle = fopen("http://localhost/sussex/assets/uploads/wc_program.csv", "r");

   $insert_str = 'INSERT INTO wc_program_temp (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES '."\n";

   while (($line_of_data = fgetcsv($file_handle, 1000, ',')) !== FALSE)
   {

   $insert_str .= '("'
         // Implode the array and fix pesky apostrophes.
         .strtr(implode('","', $line_of_data), array("'" => "''", "-" => " ", "/" => " ", "(" => " ", ")" => " "))
         .'")'."\n";
  
   //$insert_str = rtrim($insert_str, ',');

        //mysql_query($line_import_query) or die(mysql_error());
         if ($this->db->query($insert_str))
     {
        //$this->load->view('program/success');
       echo '[removed]
          alert("Document successfully uploaded and saved to the database.");
          location = "program/index";
     [removed]';
     }
     else {
       echo '[removed]
          alert("Sorry! Something went wrong please proceed to try again.");
          location = "program/upload";
     [removed]';
     }

       }
Kinda works but only inserts one(1) row not all of the CSV file and this is what I edited yesterday when we started this>
#40

[eluser]TheFuzzy0ne[/eluser]
Ugh... Try it again please. I was using a truncated version of your CSV file, and it turns out there we some double quotes towards the bottom of the file.

I now have a database table containing 1,758 rows.




Theme © iAndrew 2016 - Forum software by © MyBB