CodeIgniter Forums

Full Version: Trying to check values in another table before updating
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a table in my database for Parts, and a table for a history of changes made to Parts. The fields in the parts table are in the $data array below and the fields in the history table are named the same as in the parts table but with a '_history' suffix.

 I'm trying to make it so that when someone edits the Part (running the update_part_details() function) then the values get checked against what is in the history table and if it is different then it adds the value to the history table - that way specific fields the history table only gets updated if a change has been made to those corresponding fields. However my code below is not working. I keep getting the errors -

Undefinded Variable: data2

and further down i get another error that says 'You must use the "set" method to update an entry'

I think my problem is in my array variable(s)

here is my controller...

public function update_part_details(){
          if ($this->session->userdata('is_logged_in')) {
               $data = array (
                    'chipNumber' => $this->input->post('chipNumber'),
                    'subNumber' => $this->input->post('subNumber'),
                    'aperture' => $this->input->post('aperture'),
                    'elementsWorking' => $this->input->post('elementsWorking'),
                    'elementsNumber' => $this->input->post('elementsNumber'),
                    'dataRate' => $this->input->post('dataRate'),
                    'subarrays' => $this->input->post('subarrays'),
                    'boxLocation' => $this->input->post('boxLocation'),
                    'xLocation' => $this->input->post('xLocation'),
                    'yLocation' => $this->input->post('yLocation'),
                    'partUpdatedOn' => date("m/d/Y h:i a"),
                    'partUpdatedBy' => $this->session->userdata('email'),
                    'boxLocation' => $this->input->post('boxLocation'),
                    'geographicLocation' => $this->input->post('geographicLocation'),
                    'packageType' => $this->input->post('packageType'),
                    'packageIDNum' => $this->input->post('packageIDNum'),
                    'dateShipped' => $this->input->post('dateShipped'),
                    'partNotes' => $this->input->post('partNotes'),
                    'miscNotes' => $this->input->post('miscNotes'),
                    'instructions' => $this->input->post('instructions'),
                    'partInvestigateNotes' => $this->input->post('partInvestigateNotes'),
                    'inactive' => $this->input->post('inactive')
               $query = $this->model_parts_database_update->update_part_data($data);
               if($query) {
                    //compare values to what is in the history table
                    $history = $this->model_parts_database_read->get_part_history();
                    if ($history) {
                         foreach($history as $h) {
                              //if values are different than history table values add them as new entries to the history table
                              if ($this->input->post('chipNumber') != $h->chipNumber_history) {
                                   $data2['chipNumber_history'] = $this->input->post('chipNumber');
                              } else {
                                   $data2['chipNumber_history'] = '';
                    } else {
                    //redirect back to the part after updating all data
               } else {
          } else {

and here is my model

public function update_part_history_data($data) {
$this->db->where(''partID_FK', $this->uri->segment(5));
$query = $this->db->update('parts-db_parts-history', $data);
if ($query){
return true;
} else {
return false;

Thanks for you help!
try defining variable data2 first.

$data2 = array();