PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] Codeigniter:Update Image and Display

 January 30, 2022     codeigniter     No comments   

Issue

I'm stuck on a problem with updating image. I've created image upload which works fine but I also want it to be updated. When I add a need image it updates correctly but I if don't want to change the image and leave it as it is, then my current image can't be retrieve. Please help me

Controller

public function insert()
        {
       $data['s_em']=$this->input->post('s_em');
       $data['s_na']=$this->input->post('s_na');
    $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
             $this->load->library('upload', $config);
                                  if ( ! $this->upload->do_upload('file'))
                                      {
                              $error = array('error' => $this->upload->display_errors());

                                      }

                             else
                                 {

                        $data['file']=$this->upload->data('file_name');


                 } 

              $this->Students_m->db_ins($data);


         $this->load->view('admin/newstudents');


     }

    public function edit($id)
     {
        $dt['da']=$this->Students_m->db_edit($id)->row_array();
        $this->load->view('admin/st_edt',$dt);
      }   

public function update()
{
    $id=$this->input->post("id");

    $s_em=$this->input->post("s_em");
    $s_na=$this->input->post("s_na");

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] =     'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('file'))
    {
        $error = array('error' => $this->upload-   >display_errors());
    }
    else
    {
        $upload_data=$this->upload->data();
        $image_name=$upload_data['file_name'];
    }

$data=array('s_em'=>$s_em,'s_na'=>$s_na,'file'=>$image_name);
$this->Students_m->db_update($data,$id);
}

redirect("admin/students");
}

Model

 public function db_ins($data)
     {
    $query=$this->db->insert('student',$data);
    return $query;   
    }
public function db_edit($id)
{
    return $this->db->get_where('student', array('id'=>$id));
}
public function db_update($data,$id)
{
    $this->db->where('id', $id);       
    $this->db->update('student', $data);
}

view

  <form action="../update" method="post" enctype="multipart/form-data">

    <legend class="text-semibold">Personal data</legend>
  <img src=" <?php echo base_url( 'uploads/'. $da['file']);?>" height="205" width="205">

                                <div class="row">
                                      <div class="col-md-6">
                                            <div class="form-group">
                                        <label class="display-block">image:<span class="text-danger">*</span></label>
                                        <input name="file" type="file" id="image_id" class="file-styled ">
                                        <span class="help-block"></span>
                                    </div>
                                </div>
                    <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Email address: <span class="text-danger">*</span></label>
                                        <input type="email" name="s_em" class="form-control required"   value="<?php echo $da['s_em'];?>">
                                    </div>
                                </div>
                            </div>
                            <div class="row">

                            <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Student name: <span class="text-danger">*</span></label>
                                        <input type="text" name="s_na" class="form-control required"  value="<?php echo $da['s_na'];?>" id="n1">
                                    </div>
                                </div>
                            <button type="submit">Update<i class="icon-check position-right"></i></button>
                     <input type="hidden" name="id" value="<?php echo $da['id'] ;?>">
                    </form>
                </div>                                    

Solution

    public function update()
    {
        $id=$this->input->post("id");

        $s_em=$this->input->post("s_em");
        $s_na=$this->input->post("s_na");


 if($_FILES[file]['name']!="")
            {
    $config['upload_path'] = './uploads/';
        $config['allowed_types'] =     'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('file'))
        {
            $error = array('error' => $this->upload-   >display_errors());
        }
        else
        {
            $upload_data=$this->upload->data();
            $image_name=$upload_data['file_name'];
        }
    }
    else{
                $image_name=$this->input->post('old');
            }
$data=array('s_em'=>$s_em,'s_na'=>$s_na,'file'=>$image_name);
$this->Students_m->db_update($data,$id);
}

in the view file add the following

<input type="hidden"  id="old"  name="old"  value="<?php echo $da['file']   ?>">

try this..let me know this works or not.



Answered By - Vimal
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing