duanniu3385 2018-04-16 06:22
浏览 43
已采纳

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

I am so tried When i update my text fields and also update image then update successfully but when in update only text fields or not update image then my old image is deleted from the database in Codeigniter.

controller

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');
}

model

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'");
        }

view

<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>

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dt614037527 2018-04-16 06:50
    关注

    You are showing image in image tag so keep file field value blank

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

    and check value in post data. When you change image you will get the field value in post data

    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'");
    
    } 
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化