我正在使用 CodeIgniter,并且我有一个带有复选框的表单,允许用户检查和删除他们上传的照片。
在我的控制器中,我if(isset($checked) == 1)用来检查用户是否要删除照片。
$photo_to_table将设置为空并传递$this->tank_auth->update_user()给以执行数据库更新,并将照片字段设置为表中的空。否则,它将保持同一张照片。
但是在我的代码中,无论我是否检查,当我单击UPDATE按钮时,它会不断删除照片,我想知道为什么会这样?
有人可以通过我的代码并给出建议吗?
控制器:
$user_id = $this->session->userdata('user_id');
$profile = $this->users->get_profile_by_id($user_id);
if(!empty($_FILES['photo']['name'])) 
{
    //image upload process
}
else
{
    $checked = $this->input->post('remove');
    if(isset($checked) == 1)
        {
            $photo_to_table = '';
            // here will do remove process to delete file in directory
        }
    else
    {
        $photo_to_table = $profile->photo;
    }
}
    if($this->form_validation->run()) { // validation ok
    if(!is_null($data = $this->tank_auth->update_user(
        $this->form_validation->set_value('name'),
        $this->form_validation->set_value('country'),
        $this->form_validation->set_value('website'),
        $photo_to_table
        ))) 
    { 
        // success
        $this->session->set_flashdata('msg', 'Profile has been updated');
        redirect(current_url());
    }
}
看法:
<?php echo form_open_multipart($this->uri->uri_string()); ?>
<table border="0">
<?php
if(!empty($uphoto)){
?>
    <tr>
        <td>
            <div>
                <img src="<?php echo base_url().$userpath.$uphoto; ?>" />
           </div>
           <div>
               <input id="remove" type="checkbox" name="remove">
               <label for="remove">Remove photo</label>
           </div>
       </td>
   </tr>
<?php
}
?>
谢谢。