Welcome Guest, Not a member yet? Register   Sign In
Silent Inserts


I have a unique index on one of my table columns to avoid duplicates, what i want to do is to insert a list of items and ignoring any errors for duplicates:

$this->db_debug = FALSE;
foreach($interests as &$interest){
  $interest = trim($interest);                        
  $this->Interests_model->insert(array('interest_name' => $interest));
$this->db_debug = TRUE;

But I'm still getting

A Database Error Occurred
Error Number: 1062
Duplicate entry 'playing soccer' for key 2
INSERT INTO `interests` (`interest_name`) VALUES ('playing soccer')

What's wrong with that?

Thanks In Advance
Yehia A.Salam

Untested, but try: @$this->Interests_model->insert(array('interest_name' => $interest));

nop nothing, i got the same error, i even tried
$db['default']['db_debug'] = FALSE;
but i always get the same error.

Try this, but you may need to change the syntax but ON DUPLICATE KEY UPDATE.

INSERT INTO `interests` (`interest_name`) VALUES ('playing soccer') ON DUPLICATE KEY UPDATE (`interest_name`);


I used INSERT IGNORE instead since i don't want to perform any action when i'm inserting duplicates, it's not exposed by the Active Record Class however.

Theme © iAndrew 2016 - Forum software by © MyBB