我无法更新数据库中的数据。这是我的代码:
控制器:
public function update_contact()
{
sleep(1);
$data['validation_error'] = '';
$this -> load -> library('form_validation');
$this -> form_validation -> set_rules('name', 'Name', 'required|max_length[40]|callback_alpha_dash_space');
$this -> form_validation -> set_rules('email', 'Email', 'required|max_length[40]|valid_email');
$this -> form_validation -> set_rules('phone', 'Phone', 'required|max_length[15]|alpha_numeric');
$this->form_validation->set_rules('cbo_list', 'Group Name', 'required');
if ($this -> form_validation -> run() == FALSE) {
$message = "<strong>Editing</strong> failed!";
$this -> json_response(FALSE, $message);
}
else {
$this -> contact_model -> update($this -> input -> post('name'), $this -> input -> post('email'), $this -> input -> post('phone'), $this -> input -> post('cbo_list'), $this -> session -> userdata('uid'));
$message = "<strong>" . $this -> input -> post('name') . "</strong> has been edited!";
$this -> json_response(TRUE, $message);
redirect("site/contacts");
}
}
这是我的模特:
public function update($cid, $name, $email, $phone, $cbo_list)
{
$this->db->where(array('uid' => $this -> session -> userdata('uid'), 'cid' => $cid))
->update('contacts', array('name' => $name, 'email' => $email, 'phone' => $phone, 'gid' => $cbo_list));
}
添加信息:
我的名字=网站, 我的模型名称= contact_model, my table = contacts和group_contacts,其中contacts.gid = group_contacts.gid
并且在更新数据之前必须提及特定的UID(用户ID)。
如果我运行它,没有错误,但我无法修改数据。 你能帮助我吗?
答案 0 :(得分:1)
参数错误:
$this -> contact_model -> update($this -> input -> post('name'), $this -> input -> post('email'), $this -> input -> post('phone'), $this -> input -> post('cbo_list'), $this -> session -> userdata('uid'));
将UID移动到第一个参数位置
$this -> contact_model -> update($this -> session -> userdata('uid'), $this -> input -> post('name'), $this -> input -> post('email'), $this -> input -> post('phone'), $this -> input -> post('cbo_list'));
或者,从以下位置更改您的功能:
public function update($cid, $name, $email, $phone, $cbo_list)
到
public function update($name, $email, $phone, $cbo_list, $cid)