我正在尝试更新我的记录,它运行良好,但我在尝试修复它时遇到了一些问题(但无法做到(。
我面临的问题是,当我不想更新我的图像时(比如我只想更新名字和姓氏(,只有这些才应该更新,但当我尝试这样做时,图像名称(存储在我的数据库中(会被删除,并且没有可用的图像。
有人能告诉我该怎么做吗?
控制器部分:
public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Form_model->displayrecordsById($id);
$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]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
$this->load->view('update_records',$result);
//exit();
}
else
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$fn=$this->input->post('fname');
$ln=$this->input->post('lname');
$un=$this->input->post('username');
$em=$this->input->post('email');
if($this->upload->do_upload('filename'))
{
$fi= $this->upload->data('file_name');
}
else
{
$fi= $result['data']->filename;
}
$this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
echo 'Successfully updated your record';
//exit();
}
// else
// {
// echo $this->upload->display_errors();
// //exit();
// }
}
查看部分:
<body>
<?php
$i=1;
foreach($data as $row)
{
?>
<form method="post" enctype="multipart/form-data">
<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="<?php echo $row->filename; ?>">
<br><input type="submit" name="update" class="btn btn-success" value="Update Records"/>
</div>
</form>
<?php } ?>
</body>
模型部件
//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."'");
}
请按照下面给出的代码进行尝试:
function updaterecords($fn = '', $ln = '',$un = '', $em = '', $fi = '', $id = '')
{
$data = array();
if (isset($fn) && !empty($fn))
$data['first_name'] = $fn;
if (isset($ln) && !empty($ln))
$data['last_name'] = $ln;
if (isset($un) && !empty($un))
$data['username'] = $un;
if (isset($em) && !empty($em))
$data['email'] = $em;
if (isset($fi) && !empty($fi))
$data['filename'] = $fi;
if (!empty($data) && !empty($id))
$this->db->update('form', $data, array('ID' => $id));
}