exception_thrown
exception_thrown

Reputation: 586

Replacing image in database with edit/update function

Using codeigniter 3 So I have tried some of the examples here while searching for this issue. To no avail.

My problem is when I edit a post I am able to get all the input fields with data originally posted to display and edit accordingly. My issue is on the photo part, it uploads the image to the folder on the server, but the image name is not updated to the database so the view cannot display it.

(This issue is only on edit, on create the post it works perfect).

So what is it that I am missing here?


P.S.:: Also if someone can give me a tip on how to load the original uploaded image in the edit field so if the person decided to keep the original image they uploaded they would not need to re add it again.


My controller

public function edit($slug){
  $data['categories'] = $this->post_model->get_categories();
  $data['post_image'] = $this->post_model->get_posts('post_image');
  $data['post'] = $this->post_model->get_posts($slug);
  if(empty($data['post'])){
    show_404();
  }
$data['title'] = 'Edit Your Post';
$this->load->view('templates/header');
$this->load->view('posts/edit', $data);
$this->load->view('templates/footer');
}
public function update(){
  if(!$this->session->userdata('logged_in')){
                redirect('users/login');
            }
else{
  if($_FILES['userfile']['name'] != "")
  {
     if($_FILES['userfile']['type'] == 'image/jpeg'){
      $ext = ".jpeg";
    }
      if($_FILES['userfile']['type'] == 'image/jpg'){
       $ext = ".jpg";
     }
      else if($_FILES['userfile']['type'] == 'image/png'){
      $ext = ".png";
    }
}
    $imageedit = time();
            $config = array(
              'file_name' => $imageedit,
              'upload_path' => "assets/images/posts/",
              'allowed_types' => "jpg|png|jpeg",
            );
    $this->upload->initialize($config);
    if(!$this->upload->do_upload('userfile')){
          $this->session->set_flashdata( 'error_msg', $this->upload->display_errors());
               redirect('posts/edit');
             }
             else {
      $this->post_model->update_post($imageedit);
      $imageedit = "assets/images/posts/".$imageedit;
    }
  $slug = $this->post_model->get_posts($slug);
  $this->post_model->update_post();
  $this->session->set_flashdata('post_updated', 'Your post has been updated');
  redirect('posts');
}
}
  }

The model

public function update_post($imageedit,$slug){

  $site = $this->input->post('site');


  $data = array(

  'title'=>$this->input->post('title'),
  //'slug'=>$slug,
  'body'=> $this->input->post('Description'),
  'post_image' => $imageedit,
  'Number'=> $this->input->post('Number'),
  "Languages"=>$this->input->post('Lang'),
  'site'=>$site

  );
  $this->db->where('id',$this->input->post('id'));
  return $this->db->update('posts',$data);
}

}

?>

Upvotes: 1

Views: 787

Answers (1)

G_real
G_real

Reputation: 1157

Your image name is not updating in the database because after the else part, you are making null update request.

$this->post_model->update_post(); //Here you are passing nothing in update function.

make changes as below. because you have update function like public function update_post($imageedit,$slug)

else
{
    $imageedit = "assets/images/posts/".$imageedit;
}
$slug = $this->post_model->get_posts($slug);

$this->post_model->update_post($imageedit,$slug);  //Update this line.

At the time of edit, to display/load the original uploaded image, In your View you can do as below.

Suppose you are getting your all image data in as below in an edit function.

$data['post_image'] = $this->post_model->get_posts('post_image');
$this->load->view('posts/edit', $data);

Here, you are getting your Image data as an Array in $data['post_image'] and you are passing those all data to view file.

Add below code in View file above Browse Image code. So user can see original Image.

<?php if( $this->uri->segment(2) == 'edit' ) { ?>
Your Original uploaded Image
<img src="assets/images/posts/<?php echo $post_image[0]['post_image']; ?>" title="Image" alt="Image" />
<?php } ?>
<br />
Upload New Image => 
<?php echo form_input(array('type'=>'file','name'=>'userfile','id'=>'userfile','accept'=>'.jpg,.jpeg,.png')); } ?>

Note :- Above is just example for how to display your original image at edit time in View file. Make sure you change your images path as per your requirement.

EDIT => At time of Edit View Part, I am displaying View as below Image. So user can see the Original Image.

enter image description here

Upvotes: 1

Related Questions