2

Controller:

function edit($id)
{
    $this->form_validation->set_rules('name', 'Name', 'required|xss_clean');

    $this->load->model('manager_model');

    $pageData['type'] = 'managers';
    $pageData['title'] = "Edit Manager";
    $data['records'] = $this->manager_model->getManager($id);

    $this->load->view('header', $pageData);
    $this->load->view('db_options_view', $pageData);

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('managers/editManagerForm_view', $data);
    }
    else
    {   
        $name = $this->input->post('name');

        $this->manager_model->updateRecord($id, $name);

        $this->load->view('managers/editManagerSuccess_view');
    }

        $this->load->view('footer');
}   

Model:

function updateRecord($id, $name)
{
    $data = array('name' => $name);
    $this->db->where('id', $id);
    $this->db->update('manager', $data);
}

My problem is this. My database table doesnt accept duplicate values for the name field. So while i was testing it i entered a duplicate name and codeigniter returned the following:

A Database Error Occurred Error Number: 1062 Duplicate entry 'alan pardew' for key 2 UPDATE manager SET name = 'alan pardew' WHERE id = '2' Filename: /var/www/vhosts/mysite.com/httpdocs/personal/models/manager_model.php Line Number: 74

I would like to handle this error myself in the controller, and I have done a bit of reading up and found that I have to set $db['default']['db_debug'] = FALSE; to prevent codeigniter from displaying the errors.

Is there anyway of just turning particular errors off such as for duplicate entrys?

Or would you recommend switching them off completely?

Also..i'm fairly new to codeigniter so if you can spot any bad practice in my code feel free to point it out!

2
  • This is literally my exact situation. Cheers for saving me a job! Commented Oct 7, 2012 at 13:27
  • Another important question on this topic is: "if you are iterating over many inserts via a PHP loop, for example, will execution continue if db errors are turned off in CI, or will execution halt without simply printing an error message? Commented Mar 14, 2014 at 1:47

2 Answers 2

3

If you are using MYSQL, you can deal with duplicate keys with either "INSERT IGNORE ..." (tries to insert, if an error occurs downrank it to a warning so the query fails silently), "REPLACE INTO ..." (insert, if a duplicate keys error happens replace the old record with the new one) or "INSERT ... ON DUPLICATE KEY UPDATE ..." (insert, if a duplicate keys error happens cancel the insert and execute the update statement on the old row)

For exemple:

INSERT INTO log(id_page, view_count) VALUES (123, 1) ON DUPLICATE KEY UPDATE view_count = view_count + 1

Sadly, code igniter's active record library doesn't allow you to use that as far as I know, so the easiest way for you to deal with it would simply be to select the record to see it if already exists before inserting it (or you can execute the queries yourself without active record and check if it returns an error code).

Sign up to request clarification or add additional context in comments.

Comments

2

To check in the database CI helps:

You can write a callback_function and use it in the line of validation ex;

$this->form_validation('field','fieldname','required|callback_check');

the function would be something like this

function check($str){
    if($str exist in database){
       $this->form_validation->set_message('check','nickname in use');
       return false;
    }
    return true;
}

1 Comment

This pages also describe how to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.