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!



Messages In This Thread
Database Update Error-how to handle - by El Forum - 07-09-2012, 08:09 AM
Database Update Error-how to handle - by El Forum - 07-09-2012, 09:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB