CodeIgniter Forums
Problem with Image field emptying on editing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with Image field emptying on editing (/thread-43537.html)



Problem with Image field emptying on editing - El Forum - 07-15-2011

[eluser]the_unforgiven[/eluser]
Hi All,

I have a system with admin area and when I go to edit an event it updates the DB with the details of the text area and input box but then removes the image that I previous uploaded via the add function.

Add Event Function - This inserts all data including image correctly
Code:
function addEvent($post_data = null)
    {
        if (!$this->tank_auth->is_logged_in() ) {
            redirect('/auth/login/');
        } else {
            $data['menu']             = 'events';
            $data['title']             = 'Add Event';
            $data['user_id']        = $this->tank_auth->get_user_id();
            $data['username']        = $this->tank_auth->get_username();
            
            $data['mcd_events'] = $post_data;
            
            $this->load->view('admin/addevent', $data);
        }
    }

Edit Code - This update the title and description but removes the image
Code:
function editEvent($id = null, $post_data = null)
    {
        if (!$this->tank_auth->is_logged_in() ) {
            redirect('/auth/login/');
        } else {
            $data['menu']             = 'team';
            $data['title']             = 'Edit Rideouts/Events';
            $data['user_id']        = $this->tank_auth->get_user_id();
            $data['username']        = $this->tank_auth->get_username();
            
            // Get ID
            $id = NULL;
              if($id == NULL){
                $id = $this->uri->segment(3);
            }
            if($post_data == null) {
                $data['mcd_events'] = $this->AdminModel->getAllEvents($id);
            } else {
                $data['mcd_events'] = $data;
            }
            
            $this->load->view('admin/editevent', $data);
        }
    }


This is my save event function which does everything upon clicking the save button
Code:
function saveevent()
    {
        if (!$this->tank_auth->is_logged_in() ) {
            redirect('/auth/login/');
        } else {
            $this->load->library('session');
            $this->load->helper('date');
          // Get ID from form
          $id = $this->input->post('id');
          
          // Validation rules
          $vrules['id'] = 'required';
          $vrules['title'] = '';
          $vrules['description'] = '';
          $vrules['created_on'] = '';
          $vrules['published'] = '';
          $this->validation->set_rules($vrules);
        
          // Validation fields
          $vfields['id'] = 'ID';
          $vfields['title'] = 'Title';
          $vfields['description'] = 'Description';
          $vfields['created_on'] = 'Created On';
          $vfields['published'] = 'Published';
          $this->validation->set_fields($vfields);
        
          // Set the error delims to a nice styled red hint under the fields
          $this->validation->set_error_delimiters('<p class="hint error"><span>', '</span></p>');
            
                        
          if ($this->validation->run() == FALSE){
                // Validation failed
                if($id != "X"){
                    return $this->EditEvent($id, $_POST);
                } else {
                    return $this->AddEvent($_POST);
                }
          } else {
                
                // Validation succeeded!
                // Create array for database fields & data
                $data = array();
                $data['title'] = $this->input->post('title');
                $data['description'] = $this->input->post('description');
                $data['image'] = $this->input->post('image1');
                $data['created_on'] = date("Y-m-d");
                $data['published'] = $this->input->post('published');        
                
                              
                // Now see if we are editing or adding
                if($id == 'X'){
                  // No ID, adding new record
                  $this->AdminModel->AddEvent($data);
                }
                
                else {
                  // We have an ID, updating existing record
                  $this->AdminModel->EditEvent($id, $data);
                }
            
                // Go back to index
                redirect('admin/events/'.$this->session->userdata('last_event_page'), 'redirect');
        
          }
        }
    }

I just wondered if any of you can help me has this data is stored in a array and therfore once it's edited it goes back to the save function and obviously there's nothin in the image field once doing an edit so therefore it inserts into the DB as a blank field for the image row within the database.

Please help me

Thanks in advance.


Problem with Image field emptying on editing - El Forum - 07-15-2011

[eluser]brucebat[/eluser]
So when you goto edit details there is no image right?

It looks like there may be a problem with your Adminmodel file could you post the code?


Problem with Image field emptying on editing - El Forum - 07-15-2011

[eluser]the_unforgiven[/eluser]
It's ok after looking through the code i posted again I decided just for easy-ness I would create another function, which works a dream but thanks anyhow.


Problem with Image field emptying on editing - El Forum - 07-15-2011

[eluser]brucebat[/eluser]
Ok,

Its just im going to be making an Admin area for my website and I am curious about what I would do for it so was wondering If i could spy on your code? =)

Thanks


Problem with Image field emptying on editing - El Forum - 07-15-2011

[eluser]the_unforgiven[/eluser]
That is my admin model code so feel free to look at it