Welcome Guest, Not a member yet? Register   Sign In
Database Update Error-how to handle
#1

[eluser]reghan[/eluser]
Hello. I have a function which performs and insert, however when the user enters a value where the primary key is duplicated it gives an error. I understand why, but I am not sure how to handle it. Please see my code below. THANKS!!!!

controller:
Code:
function update_app()
{


  //set common properites.
  
  //get the id of the process from the url
  $proid = $this->uri->segment(3);
  $sysId = $this->input->post('sys');

  $sys_name = $this->department_model->get_sys_name(24);  
  $sysName= $sys_name->result();
  
  $sysn = $sysName[0]->SystemName;
  
  
  
  $res = array('SystemID' => $this->input->post('sys'),
   'ProcessId' => $this->input->post('pro'),
   'Priority' => $this->input->post('prio'),
   'RTO'=> $this->input->post('rto'),
   'RPO'=> $this->input->post('rpo'),
   'Location'=> $this->input->post('loc'),
   'Description' => $this->input->post('exp')
   );
        
        
   $flag = $this->department_model->addSystemProcess($proid,$sysId, $res);
      
    if($flag == "TRUE")  
    {  
     $this->session->set_flashdata('success', 'Application Sucessfully Added!');  
     redirect('bia/app_sys/' . $this->session->userdata('deptId'), 'refresh');
    }
    if($flag == "FALSE")
    {
    $this->session->set_flashdata('success', 'Error.!');
    $this->load->view('addApp', $data);
    }
  
  
  

}


Model:
Code:
function addSystemProcess($proId, $sysId, $resp)
{

   $this->db->where('ProcessId', $proId);
   $this->db->where('SystemID', $sysId);
   $insert = $this->db->insert('processsystem', $resp);
  
   return ($insert->db->affected_rows() > 0) ? TRUE : FALSE;
    




}

THe error which I am getting is:
Code:
Error Number: 1062

Duplicate entry '2-23' for key 'PRIMARY'

INSERT INTO `processsystem` (`SystemID`, `ProcessId`, `Priority`, `RTO`, `RPO`, `Location`, `Description`) VALUES ('23', '2', 'Vital', '', '', '', '')

THanks in advance!

#2

[eluser]boltsabre[/eluser]
Sounds like you need to split your data into two tables. Primary keys must be unique, otherwise they are not relational keys anymore!

I'd be guessing from your post you'll need a "system" table (with all the details related to systems), a "process" table with all process details, and a "join" table with three columns:
id - process_id - system_id.
Now you can populate the join table and have multiple "process_id" in there linked to many different "system_id"s.




Theme © iAndrew 2016 - Forum software by © MyBB