user warning: Duplicate entry '1' for key 'user_id' query: INSERT INTO pm_email_notify (email_notify_is_enabled, user_id) VALUES (1, 1) in sites/all/modules/privatemsg/pm_email_notify/pm_email_notify.module on line 142.

Comments

berdir’s picture

Status: Active » Needs review
StatusFileSize
new1.45 KB

db_affected_rows() is pretty much flawed, here is a better approach.

berdir’s picture

Status: Needs review » Fixed

Fixed in 6.x-1.x-dev and 6.x-2.x-dev. In 7.x-1.x, we are using db_merge() which doesn't need this fix.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

savioret’s picture

Version: » 6.x-2.x-dev
Status: Closed (fixed) » Active

It still happens in latest 6.x-2.x-dev
The function privatemsg_set_setting seems not to be using any db_merge call.

berdir’s picture

Please describe how exactly this happens.

There is no db_merge() in Drupal 6. So we probably only need to prevent any errors using @ in front of the db_query('UPDATE... line.

Patches are welcome.

savioret’s picture

Thank you, It would be great if this could be patched. I happens always that the user profile is saved and the saved value has not changed.
The problem is that mysql returns affected_rows = 0 if there was no need to update because the new value is the same as in the database (it happens when the value did not change in the update operation).

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);
  }
}

Well, other solution would be adding the @ in this line, but I think that the first one is more secure:

@db_query("INSERT INTO {pm_setting} (type, id, setting, value) VALUES ('%s', %d, '%s', %d)",$type, $id, $setting, $value);

Related: #1281350: Duplicate entry error when saving user

jgraham’s picture

Status: Active » Needs review
StatusFileSize
new695 bytes

Attached patch implements Insert db_query with '@' prefix.

Status: Needs review » Needs work

The last submitted patch, privatemsg-738880-7.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review

#7: privatemsg-738880-7.patch queued for re-testing.

ptmkenny’s picture

oadaeh’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

This issue is being closed because it is against a branch for a version of Drupal that is no longer supported.
If you feel that this issue is still valid, feel free to re-open and update it (and any possible patch) to work with the 7.x-1.x branch (bug fixes only) or the 7.x-2.x branch.
Thank you.