使用CodeIgniter上的代码更新数据而不删除旧文件 [英] Update data without deleting old files with code at CodeIgniter
问题描述
我有类似以下示例的数据,当我尝试添加新数据时,该示例想在 id上添加名称
philips
02
,但是在我更新了名称 ana之后,将jack
删除并命名为 philips
已存储,是否还有其他方法可以通过添加名称而不删除旧数据来更新表?
I have data like the example below, when I try to add new data, the example I want to add the name philips
on the id
02
but after I update the name ana, jack
to remove and the name philips
is stored, is there any other way to update the table with as add one name without deleting the old data?
id | name | class
--------------------------------------
01 | smith, john, billy | III
02 | ana, jack, | IV
控制器
$id = $this->uri->segment(3);;
$this->my_model->update_name($id);
模型
function update_name($id) {
$data = array(
'name' => $this->input->post('name'));
$this->db->where('id', $id);
$this->db->update('mytable', $data);
}
推荐答案
您可以使用MySQL CONCAT()
,在您的情况下将是这样:
You can use MySQL CONCAT()
, in your case it will be like this:
function update_name($id) {
$name = $this->input->post('name');
$this->db->set('name', "CONCAT(name,', ','".$name."')", FALSE);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
}
PS:最好不要在模型中访问您的帖子输入应该在控制器中对其进行验证,然后将其作为参数传递给模型。
P.S: Its better not to access your post input in your model, instead you should validate it in your controller and then pass it to your model as a parameter.
这篇关于使用CodeIgniter上的代码更新数据而不删除旧文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!