1

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 

7
  • ('0', '1', '2', '3') check database tables field names are correct Commented Jun 15, 2015 at 4:30
  • yes that field is not that as the error shown, but i want to know how that format of array inserted to database Commented Jun 15, 2015 at 4:33
  • can you show me your table structurer Commented Jun 15, 2015 at 4:48
  • i've been update table structure on above abdulla Commented Jun 15, 2015 at 5:26
  • you need to understand your SQL sentence. It should be INSERT INTO subscription_renewal ('id','run_date','msisdn') VALUES ('0','2015-06-15 11:10','8499270093'); Do not use insert_batch. you can use insert_string and a 'for each' loop to do this multiple row insertion Commented Jun 15, 2015 at 5:29

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

i've bee using that one also, and got below error Error Number: 1054 Unknown column 'Array' in 'field list' INSERT INTO subscription_renewal (run_date, msisdn) VALUES (Array, Array)
i need to know how to format such as that array, because the form i use with name="rund_date[]" is given an array like above i ask
you are using incorrect row set array, remap array into correct structure, see updated foreach loop block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.