CodeIgniter Forums
Silent Inserts - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Silent Inserts (/thread-23336.html)



Silent Inserts - El Forum - 10-07-2009

[eluser]yehiasalam[/eluser]
Hi,

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:

Code:
$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

Code:
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
Regards.
Yehia A.Salam


Silent Inserts - El Forum - 10-07-2009

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


Silent Inserts - El Forum - 10-07-2009

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


Silent Inserts - El Forum - 10-07-2009

[eluser]InsiteFX[/eluser]
Try this, but you may need to change the syntax but ON DUPLICATE KEY UPDATE.

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

Enjoy
InsiteFX


Silent Inserts - El Forum - 10-08-2009

[eluser]yehiasalam[/eluser]
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.