I came up here another query On which I have worked about 5 hrs but no luck.
I have a image table like below.
I have inserted the data into it successfully.
Now I want to update the inserted data by using below view, controller, and Model classes. My View class:
<form role="form" method="post" enctype="multipart/form-data" action="<?php echo base_url('admin/updateCategory'); ?>">
<div class="box-body">
<div class="form-group">
<input type="hidden" value='<?php echo $catDataForEdit->cat_id; ?>' name="id_hidden">
<label for="exampleInputEmail1">Category Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" name="category_name" placeholder="Category Name" value='<?php echo $catDataForEdit->category_name; ?>' >
</div>
<div class="form-group">
<label for="exampleInputEmail1">Upload Category Icon</label>
<!--input type="text" class="form-control" id="exampleInputEmail1" placeholder="Category Icon"-->
<p class="grey-color mt-0">Icon Size: Width: 26px, Height: 28px and Icon Format: .png</p>
<input type="file" name="cat_icon" value='<?php echo $catDataForEdit->iconName; ?>'>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Upload Category Image</label>
<p class="grey-color mt-0">Image Size: Width: 175px, Height: 120px and Image Format: .png</p>
<input type="file" name="cat_image" value='<?php echo $catDataForEdit->imageName; ?>'>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer box-footer-border">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<input type="submit" class="btn btn-primary" name="update_cat_submit" value="Modify">
</div>
</div>
my Controller Admin.php:
public function updateCategory(){
$data = array();
$targetDir = "images/dynamic/";
$prevcatIconName; $prevCatImageName;
$catIconName; $category_name; $catImageName;
$targetFilePathIcon; $targetFilePathImage;
$date; $unix_time;$hidden_id;
$hidden_id = $this->input->post('id_hidden');
if ($this->input->post('update_cat_submit')) {
$this->form_validation->set_rules('category_name', 'Category Name', 'trim|required');
if ($this->form_validation->run() == false) {
$this->modifyCategory($hidden_id);
}else{
$category_name = $this->input->post('category_name');
//Category Icon
if ( !empty($_FILES["cat_icon"]["name"])) {
//getting values from view
$catIconName = basename($_FILES["cat_icon"]["name"]);
$targetFilePathIcon = $targetDir . $catIconName;
$iconFileType = pathinfo($targetFilePathIcon,PATHINFO_EXTENSION);
//allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif');
if(in_array($iconFileType, $allowTypes)){
//upload file to server
if(move_uploaded_file($_FILES["cat_icon"]["tmp_name"], $targetFilePathIcon)){
// echo "The file ".$fileName. " has been uploaded.";
//Category Image
if ( !empty($_FILES["cat_image"]["name"])) {
$catImageName = basename($_FILES["cat_image"]["name"]);
$targetFilePathImage = $targetDir . $catImageName;
$imageFileType = pathinfo($targetFilePathImage,PATHINFO_EXTENSION);
// echo $catIconName . ','. $catImageName;
//allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif');
if(in_array($imageFileType, $allowTypes)){
//upload file to server
if(move_uploaded_file($_FILES["cat_image"]["tmp_name"], $targetFilePathImage)){
// echo "The file ".$fileName. " has been uploaded.";
$date = date('Y-m-d H:i:s');
$unix_time=human_to_unix($date);
//insert Category Icon into db and get CatIconId
$fields = array(
'image_name' => $catIconName,
'modified_at' => $unix_time,
'status' => 'a'
);
//print_r($fields);
$iconId = $this->img_model->updateImages($fields);
//echo $iconId;
//insert Category Image
$fields = array(
'image_name' => $catImageName,
'modified_at' => $unix_time,
'status' => 'a'
);
$imageId = $this->img_model->updateImages($fields);
//echo $iconId . ',' .$imageId;
echo $imageId;
//echo $unix_time;
if ($imageId != "" && $iconId != "" ) {
$next_fields = array(
'cat_name' => $category_name,
'cat_icon' => $iconId,
'cat_image' =>$imageId,
'modified_at'=>$unix_time,
'status' =>'a'
);
// print_r($next_fields); die();
$result = $this->cat_model->updateCatNames($hidden_id,$next_fields);
if ($result) {
//$this->Category();
redirect(base_url('admin/Category'));
//$this->load->view('admin/category');
}else{
$data['error_msg'] = 'there is problem with your input';
}
}
}else{
$data['error_msg'] = 'Sorry, there was an error uploading your image.';
$this->modifyCategory($hidden_id);
}//move_uploaded_file if else loop close
}else{
$data['error_msg'] = 'Sorry, only JPG, JPEG, PNG, GIF images are allowed to upload.';
$this->modifyCategory($hidden_id);
}//in_array if else loop close
}else{
$data['error_msg'] = 'Please select a image to upload.';
$this->modifyCategory($hidden_id);
}//empty file verification close
}else{
$data['error_msg'] = 'Sorry, there was an error uploading your Icon.';
$this->modifyCategory($hidden_id);
}//move_uploaded_file if else loop close
}else{
$data['error_msg'] = 'Sorry, only JPG, JPEG, PNG, GIF icons are allowed to upload.';
$this->modifyCategory($hidden_id);
}//in_array if else loop close
}else{
$data['error_msg'] = 'Please select a icon to upload.';
$this->load->view('admin/modify-category', $data);
}
}
}
}
My Model Class Img_model:
public function updateImages($fields){
$id = $this->input->post('id_hidden');
$this->db->where('id', $id);
$query = $this->db->update('images', $fields);
if($this->db->affected_rows() > 0){
$this->db->where($fields);
$result = $this->db->get('images')->row()->id;
//echo $result;
return $result;
//I have tried like below to get the last insert id but unable to get.
//return $this->db->insert_id();
}else{
return false;
}
}//close update Images
My Model Cat_model:
public function updateCatNames($hidden_id,$next_fields){
$this->db->where('id', $hidden_id);
$this->db->update('categories', $next_fields);
if($this->db->affected_rows() > 0){
return true;
}else{
return false;
}
}
I have tried to get the latest inserted record id from my Img_model class, but unable to get it. Can anyone help out please? Thanks in advance!