Welcome Guest, Not a member yet? Register   Sign In
Data is not saved when file is not attached?
#1

[eluser]whygod[/eluser]
Hi guys,

I have these codes below,

Code:
function add_record_proc()
{
  //Capture session data
  $rec_id = $this->session->userdata('rec_id');
  $action = $this->session->userdata('action');
  
  //Capture data and validate
  if($action == 'add') {
   $this->form_validation->set_rules('title', 'Title', 'trim|required|is_unique[portfolio.title]');  
  }
  
  if($action == 'edit') {
   $this->form_validation->set_rules('title', 'Title', 'trim|required');  
  }  

  $this->form_validation->set_rules('url', 'Site URL', 'trim|required');
  $this->form_validation->set_rules('description', 'Description', 'required');
  $this->form_validation->set_rules('photo_url', 'Photo', 'trim');
  $this->form_validation->set_rules('video_url', 'Video', 'trim');              
  $this->form_validation->set_rules('technology', 'Technology', 'trim');
  $this->form_validation->set_rules('buy', 'Buy', 'trim');
  $this->form_validation->set_rules('document', 'Document', 'trim');
  $this->form_validation->set_rules('document', 'Demo', 'trim');
  $this->form_validation->set_rules('download', 'Download', 'trim');

  $userfile = $this->input->post('userfile', TRUE);
  $this->data3['photo_url'] = $userfile;

  if(isset($userfile)) {
   //Upload file config
   $config['upload_path'] = './images/';
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size'] = '2048';
   $config['max_width']  = '1000';
   $config['max_height']  = '700';
   $this->load->library('upload', $config);

   //Process upload file  
   if (!$this->upload->do_upload())
   {
    //If file upload fail just do nothing.
    //$error = array('error' => $this->upload->display_errors());
    //echo $error;
    //$this->data['error_msg'] = $this->upload->display_errors();
    //$this->load->view('admin2/error_upload', $this->data);
   }
   else
   {
    //If file upload is successful capture data
    $data_file = array('upload_data' => $this->upload->data());
    $this->data3['photo_url'] = $data_file['upload_data']['file_name'];
   }
  }
  
  if ($this->form_validation->run() == FALSE)
  {
   //If validation has error display form again with error messages.  
   if($action == 'add') {
    $this->add_record();
    //redirect('admin/admin2/add_record/', 'refresh');  
   }
  
   if($action == 'edit') {
    //fech record by id segment
    $this->data['records'] = $this->folio_model->get_record_by_id($rec_id);
    $this->load->view('admin2/add_record_form', $this->data);
   }  
  }
  else
  {
   //If form validation has no error catch data
   //date('Y-m-d H:i:s');
   $this->data3['title']       = $this->input->post('title', TRUE);
   $this->data3['title_url']      = $this->input->post('url', TRUE);  
   $this->data3['description'] = $this->input->post('description', TRUE);
   $this->data3['version']     = $this->input->post('version', TRUE);
   $this->data3['stage']      = $this->input->post('stage', TRUE);
  
   $this->data3['date_started_yr'] = $this->input->post('date_started_yr', TRUE);
   $this->data3['date_started_mo'] = $this->input->post('date_started_mo', TRUE);
   $this->data3['date_started_dy'] = $this->input->post('date_started_dy', TRUE);
  
   $this->data3['last_update_yr'] = $this->input->post('last_update_yr', TRUE);
   $this->data3['last_update_mo'] = $this->input->post('last_update_mo', TRUE);
   $this->data3['last_update_dy'] = $this->input->post('last_update_dy', TRUE);
  
   $this->data3['date_finished_yr'] = $this->input->post('date_finished_yr', TRUE);
   $this->data3['date_finished_mo'] = $this->input->post('date_finished_mo', TRUE);
   $this->data3['date_finished_dy'] = $this->input->post('date_finished_dy', TRUE);

   $this->data3['video_url']  = $this->input->post('video_url', TRUE);
   $this->data3['technology']  = $this->input->post('technology', TRUE);
  
   $this->data3['buy']   = $this->input->post('buy', TRUE);
   $this->data3['document']  = $this->input->post('document', TRUE);
   $this->data3['demo']  = $this->input->post('document', TRUE);
   $this->data3['download']  = $this->input->post('download', TRUE);
   $this->data3['changelog']  = $this->input->post('changelog', TRUE);
  
   if($action == 'add') {
    if($this->folio_model->save_record($this->data3))
    {
     //If everything is okay, display success message
     $this->data['error_msg'] = '<b>Portfolio successfully saved.</b><br>';      
     $this->load->view('admin2/error_page', $this->data);
    }
    else
    {
     //Else display fail message.
     $this->data['error_msg'] = '<b>Saving Portfolio failed</b><br>';      
     $this->load->view('admin2/error_page', $this->data);
    }
   }

   if($action == 'edit') {
    $data['id']    = $rec_id;
    if($this->folio_model->update_record($this->data3))
    {
     //If everything is okay, display success message
     $this->data['error_msg'] = '<b>Portfolio successfully updated.</b><br>';      
     $this->load->view('admin2/error_page', $this->data);
    }
    else
    {
     //Else display fail message.
     $this->data['error_msg'] = '<b>Updating Portfolio failed</b><br>';      
     $this->load->view('admin2/error_page', $this->data);
    }
   }
  }
}

The codes above will work if I attached a file for upload.
The problem occur when I don't attached a file for upload the data of the form is not saved as well.

Can someone tell me why?

Thank you very much in advanced.




Theme © iAndrew 2016 - Forum software by © MyBB