Welcome Guest, Not a member yet? Register   Sign In
Only half of the data is inserted into database, but no error occurs
#1

[eluser]Mr.Data[/eluser]
Hello!
I have a very weird problem and now I am a little bit desperate because I can't find the mistake. First let me show you my code.
Code:
public function __construct() {
  parent::__construct();  
  $this->load->database();
}

public function read() {
  $j_lines = file('upload/jfile.txt');
  $j_keys = array('key1', 'key2', 'key3', 'key4');
  $this->db->trans_begin();
  foreach ($j_lines as $line) {
   $line_array = explode(";", trim($line));
   $j = array_combine($j_keys, array_slice($line_array, 0, 4));
   $this->db->insert('j', $j);
  }
  
  $k_lines = file('upload/kfile.txt');
  $k_keys = array('key1', 'key2', 'key3');
  foreach ($k_lines as $line) {
   $line_array = explode(";", trim($line));
   $k = array_combine($k_keys, $line_array);  
   $this->db->insert('k', $k);
  }
  if ($this->db->trans_status() === FALSE) {
   $this->db->trans_rollback();
   echo "error";  
  } else {
   $this->db->trans_commit();
  }
}
You see that there are two files which are imported and which lines are inserted in two different tables of the database. I am using a transaction, so if anything fails, an error will be shown and everything is rolled back.
Now here is my problem. I execute the script with the read function and no error occurs but everytime only one file is inserted into the database. So when I run the script, only the table 'j' gets some data. OK, afterwards I truncate this table and rerun the function. This time only 'k' gets some data. The next time it's again the table 'j' which gets inserted data, so the tables get alternately some data but at no time they are together filled. I already have put some "var_dump()" functions in my code but everythings seems to be OK. I know this sounds weird but does anyone of you find a mistake?




Theme © iAndrew 2016 - Forum software by © MyBB