I'm getting the following error when saving a user:

user warning: Duplicate entry '110-user-email_notify_level' for key 'PRIMARY' query: INSERT INTO pm_setting (type, id, setting, value) VALUES ('user', 110, 'email_notify_level', 12) in C:\Users\Tim\Documents\Project K\Site Design\xampplite\htdocs\sites\all\modules\_modified\privatemsg\privatemsg.module on line 3059.

Seems to be related to bugs with how db_affected_rows works:

http://drupal.org/node/805858

Attached is a quick patch which hides the error using @. Not ideal, but it's seems to be the preferred workaround...

Comments

savioret’s picture

This practice (using db_affected_rows with insert) can be dangerous. Mysql returns affected_rows = 0 if there was no need to update because the new value is the same as in the database.
The most secure is to check the number of rows and then update or insert.
In this case the insert fails because there is defined a primary key with (id, type, setting) but in other case, every update would generate a new row in the database.

This is what I think is more secure:

function privatemsg_set_setting($type, $id, $setting, $value) {
  $count = db_result(db_query("SELECT COUNT(*) FROM {pm_setting} WHERE type = '%s' AND id = %d AND setting = '%s'", $value, $type, $id, $setting));
  
  if($count){
    db_query("UPDATE {pm_setting} SET value = %d WHERE type = '%s' AND id = %d AND setting = '%s'", $value, $type, $id, $setting);
  }
  else{
    db_query("INSERT INTO {pm_setting} (type, id, setting, value) VALUES ('%s', %d, '%s', %d)",$type, $id, $setting, $value);
  }

related: #805858: Affected rows inconsistent across database engines

garciac1212’s picture

@Gmaina, I was getting the same error everytime I updated a user account. I wasted hours trying to figure out what exactly is going on.
@birwel, I kinda understand what you're saying and seems like the problem could be coming from that. But personally I'm not big on programming and couldn't see why things were conflicting. I still don't know the severity of the problem so I just applied the patch provided above and the errors went away.

Any insight in to the:
"user warning: Duplicate entry '110-user-email_notify_level' for key 'PRIMARY' query: INSERT INTO pm_setting (type, id, setting, value) VALUES ('user', 110, 'email_notify_level', 12) in C:\Users\Tim\Documents\Project K\Site Design\xampplite\htdocs\sites\all\modules\_modified\privatemsg\privatemsg.module on line 3059."

would be greatly appreciated...

ptmkenny’s picture

Status: Active » Closed (duplicate)

Duplicate of https://drupal.org/node/738880
Patch in that thread