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

[eluser]the_unforgiven[/eluser]
played around with that, but no still throws an error
#22

[eluser]TheFuzzy0ne[/eluser]
Is it the same error as before? It's possible I've made a syntax error somewhere. Try echoing $insert_str to see what's in there.
#23

[eluser]the_unforgiven[/eluser]
Yes same error as before, I checked it with echo print_r and is fine so something else is wrong., like i said the previous code i posted befroe yours works but doesn't strip out ' - _ chars
#24

[eluser]the_unforgiven[/eluser]
I changed my original to incorporate what you said:

Code:
$csv_upload = array('upload_data' => $this->upload->data());
  
   //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";

        //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]';
     }

       }
But still getting sql syntax
Quote:Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '("912049","Coastal","Yapton","Micro","Micro Asphalt Surfacing","TBC","No Positiv' at line 3

INSERT INTO wc_program_temp (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES ("911847","Coastal","Yapton","Micro","Micro Asphalt Surfacing","TBC","No Positive Traffic Management","Canal Road added PMI 16 07 12 ","2 days","0000 00 00","0000 00 00") ("912049","Coastal","Yapton","Micro","Micro Asphalt Surfacing","TBC","No Positive Traffic Management","Blenheim Road added PMI 16 07 12 ","2 days","0000 00 00","0000 00 00")

Filename: C:\xampp\htdocs\sussex\system\database\DB_driver.php

Line Number: 330

As you can see from
Code:
.strtr(implode('","', $line_of_data), array("'" => "''", "-" => " ", "/" => " ", "(" => " ", ")" => " "))
         .'")'."\n";
i have stripped everything out and just put spaces in, but still the same error.
#25

[eluser]TheFuzzy0ne[/eluser]
That's a different syntax error.

I was forgetting to put a comma on the end of each insert line. I've modified the code above. It should work now. If it doesn't, please let me know.
#26

[eluser]the_unforgiven[/eluser]
Weird one this...

But still syntax error:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

INSERT INTO wc_program_temp (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES ("911847","Coastal","Yapton","Micro","Micro Asphalt Surfacing","TBC","No Positive Traffic Management","Canal Road added PMI 16 07 12 ","2 days","0000 00 00","0000 00 00"),

Filename: C:\xampp\htdocs\sussex\system\database\DB_driver.php

Line Number: 330
#27

[eluser]TheFuzzy0ne[/eluser]
I think the problem is that I've used double-quotes. Please try my revised code (again...). If it doesn't work, I'm going to weep softly into my pillow as I cry myself to sleep tonight...
#28

[eluser]the_unforgiven[/eluser]
Looks like you will have to cry mate, but dry your eyes, cos it still don't work Sad
#29

[eluser]TheFuzzy0ne[/eluser]
Aw! What's the error now?

I might have to see if I can get your code setup and working at my end. It would certainly be easier to debug. Any chance you could give me access to some sample CSV data and your database table structure?
#30

[eluser]the_unforgiven[/eluser]
Attached is the csv file i'm using which is dummy data.

An you have the code right?




Theme © iAndrew 2016 - Forum software by © MyBB