php - Code Igniter-批量插入功能在数据库中插入空字段

标签 php mysql codeigniter

每当我尝试插入数据时,两列的字段均为空。只有自动递增(当然)MainReqID 和最后一列才有字段。 这是我的 Controller ..

public function insert_main($data,$orgs){
  $this->db->insert('insert_main',$data);
  $getID = $this->db->insert_id();

  $ctr=1;
  $insertorg = array();
  foreach($i=0; $i<count($orgs); $i++){
     $insertorg[] = array(
       'OrgID'=>$ctr[$i],
       'MainID'=>$getID[$i],
       'Level'=>'1234'[$i]
     );
  $ctr++;
  }
  $this->db->insert_batch('insert_mainreq',$insertorg);
}

这是它在数据库中的样子...

MainReqID | OrgID | MainID | Level
1         | null  | null   | 1234
2         | null  | null   | 1234
3         | null  | null   | 1234
4         | null  | null   | 1234
5         | null  | null   | 1234.. and so on..

我需要这样的东西..

MainReqID | OrgID | MainID | Level
1         | 1     | 3      | 1234
2         | 2     | 3      | 1234
3         | 3     | 3      | 1234
4         | 4     | 3      | 1234
5         | 5     | 3      | 1234.. and so on..

最佳答案

看起来$getID不是一个数组,但您正在添加$getID[i]。这肯定行不通。与 $ctr 相同。这是一个整数,但您正在尝试 $ctr[i]。同样的事情也发生在 Level 上。

public function insert_main($data,$orgs){
  $this->db->insert('insert_main',$data);
  **$getID** = $this->db->insert_id();

  **$ctr=1;**
  $insertorg = array();
  foreach($i=0; $i<count($orgs); $i++){
     $insertorg[] array(
       'OrgID'=>**$ctr[$i]**,
       'MainID'=>**$getID[$i]**,
       'Level'=>**'1234'[$i]**
     );
  $ctr++;
  }
  $this->db->insert_batch('insert_mainreq',$insertorg);
}

你可以尝试这个,我不确定你想用 OrgId 和 MainID 做什么,但是:

public function insert_main($data,$orgs){
  $this->db->insert('insert_main',$data);
  $getID = $this->db->insert_id();

  $insertorg = array();
  foreach($i=0; $i<count($orgs); $i++){
     $insertorg[] array(
       'OrgID'=> $i,
       'MainID'=>$getID,
       'Level'=>'1234'
     );
  }
  $this->db->insert_batch('insert_mainreq',$insertorg);
}

请记住,如果有多行,$this->db->insert_id(); 将返回插入的最后一行的 ID。

关于php - Code Igniter-批量插入功能在数据库中插入空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46029556/

相关文章:

php - cakephp 中查找查询出错

php - 如何在 CodeIgniter 2 中使用 PDO?

PHP date_diff 函数返回错误结果

php - 如何用大写字母分隔单词?

php - 我可以在我的项目中使用 zend 翻译、日期和缓存作为独立类吗?

multithreading - 使用PHP CLI SAPI的基于PHP的服务器

php - 检查 while 循环键是否存在于单独的数组中

php - UTF-8贯穿始终

php - 如何在 ci4 路由中传递任意数量的参数?

php - Codeigniter 事件记录错误?查询构建器添加了一行