【问题标题】:Insert Data from array to database on Codeigniter在 Codeigniter 上将数据从数组插入数据库
【发布时间】:2015-08-30 11:33:39
【问题描述】:

我想用 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

表结构

 创建表subscription_renewal (
  id int(11) NOT NULL AUTO_INCREMENT,
  msisdn varchar(32) 字符集 utf8 非空,
  service varchar(64) 字符集 utf8 非空,
  adn varchar(8) 字符集 utf8 非空,
  operator varchar(32) 字符集 utf8 非空,
  channel varchar(16) 字符集 utf8 非空,
  status tinyint(4) 非空,
  description varchar(20) 字符集 utf8 默认为 NULL,
  blacklist_status tinyint(4) 非空,
  date_created 日期时间不为空,
  date_modified 日期时间不为空,
  run_date 日期时间默认为空,
  price varchar(30) 默认为空,
  主键 (id)
) ENGINE=InnoDB AUTO_INCREMENT=476 默认字符集=latin1 

【问题讨论】:

  • ('0', '1', '2', '3')检查数据库表字段名称是否正确
  • 是的,该字段不是显示的错误,但我想知道该数组格式是如何插入数据库的
  • 你能给我看看你的表结构器吗
  • 我一直在更新 abdulla 上面的表结构
  • 你需要理解你的SQL语句。应该是INSERT INTO subscription_renewal ('id','run_date','msisdn') VALUES ('0','2015-06-15 11:10','8499270093'); 不要使用insert_batch。您可以使用 insert_string 和“for each”循环来执行此多行插入

标签: arrays codeigniter


【解决方案1】:

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

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

【讨论】:

  • 我也用过那个,但得到以下错误错误号:1054 Unknown column 'Array' in 'field list' INSERT INTO subscription_renewal (run_date, msisdn) VALUES (数组,数组)
  • 我需要知道如何格式化诸如那个数组,因为我使用 name="rund_date[]" 的形式给出了一个像上面我问的数组
  • 您使用了不正确的行集数组,将数组重新映射到正确的结构中,请参阅更新的 foreach 循环块
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
  • 2014-05-04
  • 2017-01-25
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多