在Codeigniter上将数据从数组插入数据库

时间:2015-06-15 04:14:59

标签: arrays codeigniter

我希望使用codeigniter框架从数组格式插入数据数组。

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 ) ) 

我一直在尝试在codeigniter上使用insert_batch命令,但它根本不起作用。如下所示。

我的控制器

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

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

}

和我的模特

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

错误显示:

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')

文件名:C:\ xampp \ htdocs \ msisdn_tools_new \ system \ database \ DB_driver.php行号:330

表格结构

  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 

1 个答案:

答案 0 :(得分:2)

插入批处理数组结构看起来不正确,您应该将输入数据传递到每行的set数组中...请参阅示例数组结构

 $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);