Welcome Guest, Not a member yet? Register   Sign In
catch db errors before returning back to controller

I have an update form which I am using to update info in my database.
The account_ num is a primary key but it can be updated if user enters it wrong. I tried using
$this->form_validation->set_rules('acc_num','Account Number','trim|required|is_unique[patient_info.account_num]|xss_clean');
But using this it will not let me update the field. So I removed the line, but when it get to the update part of my code codeigniter redirects me to a page which displays this info
Error Number: 1062

Duplicate entry '2' for key 'PRIMARY'

UPDATE `ci_patient_info` SET `account_num` = '2', `dob` = '12-12-1955', `sex` = 'Male' WHERE `account_num` = 1

Filename: C:\wamp\www\drz\sys\database\DB_driver.php

Line Number: 330
How would I catch this error so I can display my own error message in the view page

In Controller
$this->form_validation->set_rules('acc_num','Account Number','trim|required|is_unique[patient_info.account_num]|callback_check_duplicate|xss_clean');

  if ($this->form_validation->run() == FALSE)
   // edit view page
                        // update record

public function check_duplicate($str)
   $this->form_validation->set_message('check_duplicate', '<div class="msg failure"><span>record already exists..</span></div>');
   return false;
   return true;
Try this code.


I tried this but if I keep the is_unique[patient_info.account_num] part of the set_rules function it will never hit the check_duplicate function. So I removed it and know I get this error
A PHP Error was encountered
Severity: Notice

Message: Undefined property: Patients::$duplicate_mod

Filename: controllers/patients.php

Line Number: 97
( ! ) Fatal error: Call to a member function check_unique() on a non-object in C:\wamp\www\drz\app\controllers\patients.php on line 97
Call Stack
# Time Memory Function Location
1 0.0010 389320 {main}( ) ..\index.php:0
2 0.0029 462288 require_once( 'C:\wamp\www\drz\sys\core\CodeIgniter.php' ) ..\index.php:202
3 0.5900 3913632 call_user_func_array ( ) ..\CodeIgniter.php:331
4 0.5900 3913680 Patients->update_patient_info( ) ..\CodeIgniter.php:331
5 0.5905 3916680 CI_Form_validation->run( ) ..\patients.php:78
6 0.6097 3922248 CI_Form_validation->_execute( ) ..\Form_validation.php:341
7 0.6101 3922976 Patients->check_duplicate( ) ..\Form_validation.php:593

create model with the name of duplicate_mod and create a function in that file
function check_unique($str)
        $query = $this->db->where("fieldname",$str);
        $query = $this->db->get('table_name');
        if ($query->num_rows() > 0) {
           return FALSE;
           return TRUE;

Theme © iAndrew 2016 - Forum software by © MyBB