Welcome Guest, Not a member yet? Register   Sign In
Database Insert Skip field if blank
#1

[eluser]Unknown[/eluser]
Hi All,

I'm very new to CI and am trying to create a profile update for users profiles. As part of that I have a field where they can upload a profile picture.

The update uploads the file and inserts the file name into the database all correctly so that's fine, however I stumbled upon a problem where if the user goes back and then updates their profile without uploading a new picture the update then inserts a null into the picture field.

I've tried code to say only insert the file name is blank, I've tried code where the file name is inserted into a hidden field and if they don't upload a new field then the value of the hidden field is inserted into the database, but none of that works.

This is the code in my controller :

Code:
function update()
{
  $this->load->model('profile_model');
  $this->profile_model->do_upload();
  $entries = $this->profile_model->update_profile();
  $this->index($this->input->post('id'));
}

This is my model
Code:
class Profile_model extends CI_Model {

var $gallery_path;
var $gallery_path_url;

function Profile_model() {

  $this->gallery_path = realpath(APPPATH . '../profile-pictures');
  $this->gallery_path_url = base_url().'profile-pictures/';
}
function do_upload() {

  $config = array(
   'allowed_types' => 'jpg|jpeg|gif|png',
   'upload_path' => $this->gallery_path,
   'max_size' => 2000
  );

  $this->load->library('upload', $config);
  $this->upload->do_upload();
  $image_data = $this->upload->data();
  
  $config = array(
   'source_image' => $image_data['full_path']
  );

  $this->load->library('image_lib', $config);
  
}

function update_profile()
{
  $data = array(
   'firstName' => $this->input->post('firstName'),
   'lastName' => $this->input->post('lastName'),
   'username' => $this->input->post('username'),
   'emailAddress' => $this->input->post('emailAddress'),
   'studioName' => $this->input->post('studioName'),
   'publicAddress1' => $this->input->post('publicAddress1'),
   'publicAddress2' => $this->input->post('publicAddress2'),
   'publicAddress3' => $this->input->post('publicAddress3'),
   'townCity' => $this->input->post('townCity'),
   'publicAddress4' => $this->input->post('publicAddress4'),
   'zipCode1' => $this->input->post('zipCode1'),
   'zipCode2' => $this->input->post('zipCode2'),
   'stateid' => $this->input->post('stateid'),
   'regionid' => $this->input->post('usregions'),
   'countryID' => $this->input->post('country'),
   'publicTelephone' => $this->input->post('publicTelephone'),
   'publicCellPhone' => $this->input->post('publicCellPhone'),
   'publicEmail' => $this->input->post('publicEmail'),
   'websiteAddress' => $this->input->post('websiteAddress'),
   'skypeLesson' => $this->input->post('skypeLesson'),
   'profileInformation' => $this->input->post('profileInformation'),
   'lessonOtherCountry' => $this->input->post('lessonOtherCountry'),
   'privateAddress1' => $this->input->post('privateAddress1'),
   'privateAddress2' => $this->input->post('privateAddress2'),
   'privateAddress3' => $this->input->post('privateAddress3'),
   'privateTown' => $this->input->post('privateTown'),
   'privateAddress4' => $this->input->post('privateAddress4'),
   'privateZipCode' => $this->input->post('privateZipCode'),
   'privatePhone' => $this->input->post('privatePhone'),
  );
  
  $image_data = $this->upload->data();
  
  
  $data['profilePicture'] = $image_data['file_name'];
  
  
  if($this->input->post('password') && $this->input->post('password') != "") {
   $data['password'] = md5($this->input->post('password'));
  }

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

and my view
Code:
<ul>
  <li>
    <label class="desc">Profile Picture</label>
    <div>
    &lt;?php echo form_upload('userfile'); ?&gt;
    
    </div>
    &lt;?php echo form_hidden('hidden_picture',  $profile_data->profilePicture) ?&gt;
  </li>
  </ul>

I know my upload validation isn't brilliant but I just wanted to get it working first and then I can make it a bit tighter.

I know this probably something simple but it is alluding me just now.

Can anyone help?
#2

[eluser]vitoco[/eluser]
why not just put it in a conditional block if the image exists ??
Code:
$image_data = $this->upload->data();
    // IF IMAGE WAS UPLOADED  
    if( isset( $image_data['file_name'] ) AND $image_data['file_name'] != '' ) // THIS IS THE GENERAL IDEA
   {
      $data['profilePicture'] = $image_data['file_name'];
   }

Saludos
#3

[eluser]Unknown[/eluser]
[quote author="vitoco" date="1340786781"]why not just put it in a conditional block if the image exists ??
Code:
$image_data = $this->upload->data();
    // IF IMAGE WAS UPLOADED  
    if( isset( $image_data['file_name'] ) AND $image_data['file_name'] != '' ) // THIS IS THE GENERAL IDEA
   {
      $data['profilePicture'] = $image_data['file_name'];
   }

Saludos[/quote]

That's just perfect and exactly what I needed! Thank you so much. I knew the logic of what I was trying to do I just couldn't express it in code and this worked like a charm!

#4

[eluser]vitoco[/eluser]
i'm glad it helped




Theme © iAndrew 2016 - Forum software by © MyBB