codeigniter - 在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.|
解决方案
我仍然不完全理解您想要做什么,但是如果您想获取某列为空或为空的内容:
$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');
推荐阅读
- javascript - 依赖于 httprequest 的 ag-grid 数据有时不会加载表
- meteor - 更新 Meteor 版本时出错
- docker - 找不到:NGINX 入口
- swiftui - UIKit for SwiftUI 中 applicationDidReceiveMemoryWarning 的替代方法是什么?
- python - CSV 文件问题。我转换为整数,但仍然得到 TypeError: unsupported operand type(s) for -: 'str' and 'str'
- python-3.x - Python Pandas 中乱码的正则表达式替换
- python - 为什么我的函数会触发多次?
- azure-data-explorer - 如何使用条件执行不同的查询以在 Kusto 中查找特定子字符串?
- html - 在 React 应用程序中,当图像 url 是对象的字符串属性时,图像无法显示
- javascript - ReactJS - 我可以生成多个具有共享相同名称但不同值的值属性的表单字段吗?