0

I came up here another query On which I have worked about 5 hrs but no luck. I have a image table like below. My Images table

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!

DogWalkersCoding
  • 103
  • 1
  • 10
Madhu Kumar
  • 175
  • 1
  • 4
  • 15
  • http://stackoverflow.com/questions/16440366/how-to-get-last-insert-id-after-insert-query-in-codeigniter-active-record http://stackoverflow.com/questions/20310298/get-the-id-of-the-last-updated-record – shafiq.rst Mar 07 '17 at 05:27
  • Thanks for reply, I tried the second one I ma getting error undefined: result_array(). – Madhu Kumar Mar 07 '17 at 05:34

3 Answers3

0

Try this:

if( $this->db->update('categories', $next_fields) )
{
    echo $hidden_id;
}

As $this->db->update() return TRUE / FALSE on successful / failed execution of query. So the above code echo $hidden_id only when query successfully executes.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

First Try this

Second, Your table images doesn't have a cat_id field in it. How are you creating a relation between images and categories table. See the example image below

enter image description here

I have a property_images table and i have property_id in this way I can have multiple images of the property too. But since you can have only one category image putting a category_id in your images table would make your query structure a lot simpler.

You just have to update the record which has the category ID you are sending and return the full record in case you want to get the ID of the record from it.

Your Case

     $fields = array(
     'image_name' => $catImageName,
     'modified_at' => $unix_time,
     'status'     => 'a'
     );
     $imageId = $this->img_model->updateImages($fields);

You are using above code to update image table where you are sending category Id from your view

<input type="hidden" value='<?php echo $catDataForEdit->cat_id; ?>' name="id_hidden">

and in your image model you are getting that category id to update images record

$id = $this->input->post('id_hidden');

$this->db->where('id', $id);
$query = $this->db->update('images', $fields);

And now you want to get the image ID which you updated , which you can get it by this piece of code

 $st=$this->db->select('*')->from('images')->WHERE('image_name',$fields['image_name'])->get()->result_array();
 $updatedRecordId=$st[0]['id']
 // Now you can return it or use it. 

But that doesn't make any sense to me.

Community
  • 1
  • 1
Mudassar Khani
  • 1,469
  • 1
  • 20
  • 39
0

On your 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){
        return $id;
        // $id is your updated record id because it is unique.
   }else{
    return false;
   }   
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Nitesh Gupta
  • 108
  • 6