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