首页 > 解决方案 > 在codeigniter中插入后更新多行

问题描述

插入后,我想用($id.'mytext')db 中的 where 更新字段,该字段对于所有行都是空的。

table name: peca
columns: 
id -autoincrement
A -varchar user insert
CB -varchar auto insert with update
MODEL WILL RETURN ALL ROWS WHERE CB=empty 

function model1() {
        $this->db->select('*');
        $this->db->from('peca');
        $this->db->where('CB', '');
        //$this->db->where('id', $fileid); 
        $query = $this->db->get();
        if ($query) {
            return $query->result();
        } else {
            return false;
        }
    }
MODEL WILL update in db where CB=empty 
function model2($dados = NULL) {
        if ($dados !== NULL) {
        //  extract($dados);
         $this->db->where('CB', '');
            $this->db->update('peca', $dados);
            return true;
        } else {
            return false;
        }
    }
CONTROLLER

 $this->load->model('model_peca');
            $resultadocadastropeca = $this->model_peca->model1($id);
            $data = 'id' => $id;
            $appointment = array('codigoean' => $data.'.PECA.');    
            $appointment = $this->model_peca->model2($appointment);

START TABLE 从导入中插入以前的值,因此 CB 只能在 id 存在后生成

id|CB      |
22|        |
31|        |

结果 我将 CB 更改为 .PECA。在 CB=empty 但每行的 $id 未通过的所有行中

id|CB    |
22|.PECA.|
31|.PECA.|

预期的

id|CB      |
22|22.PECA.|
31|31.PECA.|

标签: codeigniterrows

解决方案


我仍然不完全理解您想要做什么,但是如果您想获取某列为空或为空的内容:

$this->db->where("WHERE yourColumn IS NULL OR yourColumn = ''");


根据您修改后的问题,这应该有效:

$this->db->select('id');
$this->db->where('CB', '');
$q = $this->db->get('peca');
if ($q->num_rows() == 0) {
   die('nothing to do here');
}
$res = $q->result();
foreach ($res as $item) {
     $data[] = array('id' => $item->id, 'CB' => $item->id . '.PECA.');
}
$this->db->update_batch('peca', $data, 'id');

推荐阅读