Amir Rachman
Amir Rachman

Reputation: 225

Insert Data from array to database on Codeigniter

i want to insert data an array, from array format like this with codeigniter frameworks.

Array ( [run_date] => Array ( [0] => 2015-06-15 11:10 [1] => 2015-06-15 11:10 [2] => 2015-06-15 11:10 [3] => 2015-06-15 11:10 ) [msisdn] => Array ( [0] => 8499270093 [1] => 8599387282 [2] => 6281019183 [3] => 8597375112 ) ) 

i've been trying to use insert_batch command on codeigniter but it's not works at all. such like below.

My Controller

function insertFromConfirmation() {
    $datanew = array(
       'run_date' => $this->input->post('run_date'),
       'msisdn' => $this->input->post('msisdn')
    );

    print_r($datanew);  
    $this->modelMsisdn->insertDataArray($datanew);

}

and My Model

public function insertDataArray($datanew) {
    $this->db->insert_batch('subscription_renewal', $datanew);
}

Error Shown:

Error Number: 1054 Unknown column '0' in 'field list' INSERT INTO `subscription_renewal` (`0`, `1`, `2`, `3`) VALUES ('2015-06-15 11:10','2015-06-15 11:10','2015-06-15 11:10','2015-06-15 11:10'), ('8499270093','8599387282','6281019183','8597375112')

Filename: C:\xampp\htdocs\msisdn_tools_new\system\database\DB_driver.php Line Number: 330

Table Structure

  CREATE TABLE subscription_renewal (
  id int(11) NOT NULL AUTO_INCREMENT,
  msisdn varchar(32) CHARACTER SET utf8 NOT NULL,
  service varchar(64) CHARACTER SET utf8 NOT NULL,
  adn varchar(8) CHARACTER SET utf8 NOT NULL,
  operator varchar(32) CHARACTER SET utf8 NOT NULL,
  channel varchar(16) CHARACTER SET utf8 NOT NULL,
  status tinyint(4) NOT NULL,
  description varchar(20) CHARACTER SET utf8 DEFAULT NULL,
  blacklist_status tinyint(4) NOT NULL,
  date_created datetime NOT NULL,
  date_modified datetime NOT NULL,
  run_date datetime DEFAULT NULL,
  price varchar(30) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=476 DEFAULT CHARSET=latin1 

Upvotes: 1

Views: 182

Answers (1)

Girish
Girish

Reputation: 12127

Insert batch array structure looking incorrect, you should pass input data into set array of each row... see sample array structure

 $run_date = $this->input->post('run_date');
 $msisdn = $this->input->post('msisdn');
 $datanew = array();
 foreach($run_date as $k => $v){
       $datanew[] = array(
           'run_date' => $v,
           'msisdn' => $msisdn[$i] //suppose $msisdn[] have also same key length as $run_date[] array
           );
 }
 $this->modelMsisdn->insertDataArray($datanew);

Upvotes: 2

Related Questions