Codeigniter中的文本更新和图像问题

时间:2018-04-16 06:22:08

标签: php jquery ajax codeigniter

我是如此尝试当我更新我的文本字段并更新图像然后成功更新但在仅更新文本字段或不更新图像时,我的旧图像将从Codeigniter中的数据库中删除。

控制器

function blog_walk_update($id){

    $this->_checkAuthorized(self::CATEGORY_ID,'w');
    $_POST['id'] = $id; 
    //start
    if($_FILES['file_name']!=""){
        $storeFolder = './photo/uploads/';
        $pics = array();
        $file = $_FILES['file_name']['name'];
        $tempFile = $_FILES['file_name']['tmp_name'];
        $targetPath = $storeFolder .DIRECTORY_SEPARATOR;
        $targetFile =  $targetPath. $file;
        if(move_uploaded_file($tempFile,$targetFile)){
            array_push($pics,array("img"=>"/".$targetFile));
        }
        $_POST['file_name'] = $file;
    }else{
        $_POST['file_name'] = $file;
    }

    /////end

    $this->blog->blog_walk_update($_POST);
    redirect('/admin/blog/walk');
}

型号

function blog_walk_update($data){
            $id = addslashes($data['id']);
            $t_name = addslashes($data['t_name']);
            $t_desp = addslashes($data['t_desp']);
            $file_name = addslashes($data['file_name']);
            $area_id = addslashes($data['area_id']);
            return $this->db->query("UPDATE walk_area  SET t_name = '$t_name', t_desp = '$t_desp', area_id = '$area_id',file_name = '$file_name'  WHERE id = '$id'");
        }

查看

<div class="form-group">
    <label class="col-sm-2 control-label">Upload Image</label>
    <div class="col-sm-4">
        <img src="<?php echo base_url()."photo/uploads/".$walks->file_name  ?>" style ="width: 100px;">    
        <input type="file" value="<?= $walks->file_name; ?>" name="file_name" id="file" /><br/>
    </div>
</div>

提前致谢

1 个答案:

答案 0 :(得分:3)

您正在图像标记中显示图像,因此请将文件字段值保留为空白

<input type="file" value="" name="file_name" id="file" /><br/>

并检查发布数据中的值。更改图像时,您将获得后期数据中的字段值

function blog_walk_update($data){

$id = addslashes($data['id']);

$t_name = addslashes($data['t_name']);

$t_desp = addslashes($data['t_desp']);

$area_id = addslashes($data['area_id']);

if(isset($data['file_name']) && $data['file_name']!=''){

$file_name = addslashes($data['file_name']);
    return $this->db->query("UPDATE walk_area  SET t_name = '$t_name', t_desp = '$t_desp', area_id = '$area_id',file_name = '$file_name'  WHERE id = '$id'");

}else{

    return $this->db->query("UPDATE walk_area  SET t_name = '$t_name', t_desp = '$t_desp', area_id = '$area_id'  WHERE id = '$id'");

} 

}