希望我能好好解释一下...
我在 Codeigniter 中使用表单验证。 如果我的表单返回一个字符串 id 返回给 Controller ,我如何使用它来执行数据库查询并使用结果数组来执行更新...
这有意义吗?
function form($id) {
if ($this->form_validation->run('') == false) {
//...
}
else {
// Query database where strId = $this->input->post('strId')
// Put results into an array called $data
// Update database where id = $this->input->post('id')
$this->db->where('id', $this->input->post('id'));
$this->db->update('database', $data);
}
}
最佳答案
Controller :
//... (if valid) ...
$this->load->model('myform'); //loads myform_model.php
$this->myform->update(); //call the update method
模型(myform_model.php):
function update(){
$this->load->database();
$this->db->where('id', $this->input->post('id'));
$this->db->update('tableName', $data);
}
注意:您的 ID 必须已经存在于数据库表中,因为您正在使用“更新”。要插入数据,请使用 $this->db->insert();
关于php - Controller 内查询的 Codeigniter 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18882587/