Php 如何使用codeigniter更新记录?
我正在尝试更新记录,但我不知道为什么我的记录没有得到更新。我正在尝试,但没有得到想要的结果。甚至验证也不起作用 控制器部件Php 如何使用codeigniter更新记录?,php,codeigniter,Php,Codeigniter,我正在尝试更新记录,但我不知道为什么我的记录没有得到更新。我正在尝试,但没有得到想要的结果。甚至验证也不起作用 控制器部件 public function updatedata() { $id=$this->input->get('id'); $result['data']=$this->Form_model->displayrecordsById($id); $this->load->view('update_records',$re
public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Form_model->displayrecordsById($id);
$this->load->view('update_records',$result);
if($this->input->post('update'))
{
$this->form_validation->set_rules('fname', 'First name', 'required');
$this->form_validation->set_rules('lname', 'Last name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[form.username]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[form.email]');
if ($this->form_validation->run() == TRUE)
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if($this->upload->do_upload('filename'))
{
$fn=$this->input->post('fname');
$ln=$this->input->post('lname');
$un=$this->input->post('username');
$em=$this->input->post('email');
$fi= $this->upload->data('file_name');
$this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
echo 'Successfully updated your record';
}
}
}
}
请尝试以下代码以帮助您查明错误。但很可能您没有定义$id
public function updatedata()
{
$id=$this->input->get('id');
if($this->input->post('update'))
{
$this->form_validation->set_rules('fname', 'First name', 'required');
$this->form_validation->set_rules('lname', 'Last name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[form.username]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[form.email]');
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
exit();
} else {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if($this->upload->do_upload('filename'))
{
$fn=$this->input->post('fname');
$ln=$this->input->post('lname');
$un=$this->input->post('username');
$em=$this->input->post('email');
$fi= $this->upload->data('file_name');
// $id is not defined below.... you will need to define this I would imagine!
$this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
echo 'Successfully updated your record';
exit();
} else {
echo $this->upload->display_errors();
exit();
}
}
}
$result['data']=$this->Form_model->displayrecordsById($id);
$this->load->view('update_records',$result);
}
代码的问题是,如果
部件出现故障,您永远不会让其他部件显示错误,如果你的验证不起作用,那么它将如何进行更新?你能编辑我的代码并告诉我吗?先生,我正在尝试一切,但它不起作用。请你帮我一点忙吗mw@AnbuselvanRocky谢谢你的编辑,我已经批准了,但是还有很多地方需要改进(请参阅修订历史记录)。您现在应该有一个错误…请告诉我该错误是什么-帮助我帮助您-提供尽可能多的信息!您的提交按钮是否已将名称属性设置为“更新”?在代码的不同位置设置一些断点,以查看命中的内容和未命中的内容….die(1);
,die(2)
等…先生,如果我试图编辑我的记录,我会收到验证错误,例如用户名字段必须包含唯一值。电子邮件字段必须包含唯一值。太好了!您刚刚确认了您的代码正在工作,并像应该的那样捕获验证错误!现在完全按照错误所说的那样执行。使用不同的用户名和电子邮件为了测试!先生,当我上传所有内容时,我也成功地收到了一条消息,但后来我遇到了一个PHP错误严重性:注意消息:未定义变量:id文件名:controllers/Form.PHP行号:128 Backtrace:File:C:\xampp\htdocs\amit\application\controllers\Form.PHP行:128函数:\错误\处理程序文件:C:\xampp\htdocs\amit\index.php行:315函数:需要\u一次