我创建了具有正确验证的注册表格,并将其插入数据库中。现在我正在尝试更新记录,但我不知道为什么我的记录没有得到更新。请任何人让我知道我的记录未更新可能是什么问题。请从早上开始尝试,但无法获得理想的结果。即使验证也不起作用。请任何人查看我的代码,然后告诉我。
From.php(控制器)
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';
}
}
}
}
From_model.php
//get values
function displayrecordsById($id)
{
$query=$this->db->query("select * from form where ID='".$id."'");
return $query->result();
}
//update record
function updaterecords($fn,$ln,$un,$em,$fi,$id)
{
$query=$this->db->query("update form SET first_name='$fn',last_name='$ln',username='$un',email='$em',filename='$fi' where ID='".$id."'");
}
update_records.php
<body>
<?php
$i=1;
foreach($data as $row)
{
?>
<?php echo form_open_multipart(''); ?>
<div class="container">
<h5 style="color:#ff1a1a">First name</h5>
<input type="text" class="form-control" name="fname" value="<?php echo $row->first_name; ?>"/>
<span class="text-danger"><?php echo form_error("fname");?></span>
<h5 style="color:#ff1a1a">Last name </h5>
<input type="text" class="form-control" name="lname" value="<?php echo $row->last_name; ?>"/>
<span class="text-danger"><?php echo form_error("lname");?></span>
<h5 style="color:#ff1a1a">Username</h5>
<input type="text" class="form-control" name="username" value="<?php echo $row->username; ?>"/>
<span class="text-danger"><?php echo form_error("username");?></span>
<h5 style="color:#ff1a1a">E-mail</h5>
<input type="text" class="form-control" name="email" value="<?php echo $row->email; ?>"/>
<span class="text-danger"><?php echo form_error("email");?></span>
<h5 style="color:#ff1a1a">Image</h5>
<img src="<?php echo base_url('uploads/' . $row->filename);?>" class="img-responsive" alt="image" width="100px" height="100px"><br><input type="file" name="filename" value="">
<br><input type="submit" name="update" class="btn btn-success" value="Update Records"/></td>
</div>
</form>
<?php } ?>
</body>
答案 0 :(得分:0)
确保将表单验证库加载到控制器中
unsigned crc24_rem(unsigned crc, unsigned val, unsigned bits) {
val &= 0x100 - (0x100 >> bits) ;
crc ^= (unsigned)val << 16;
while (bits--)
crc = crc & 0x800000 ? (crc << 1) ^ 0x864cfb : crc << 1;
crc &= 0xffffff;
return crc;
}
在您的模型中,您也应该尝试以下查询:
$this->load->library('form_validation');
如果您仍然没有得到任何帮助,请尝试$queryString ="update form SET first_name='?, last_name= ?, username=?, email=?, filename=? where ID=?"
$this->db->query($queryString, array($fn,$ln,$un,$em,$fi,$id));
,以确保您实际上正在获取您的帖子数据。
让我知道这是否有帮助。