When a user account is deleted, the "poll_user" function below is executed where it updates the "{poll_votes}" table to set the UID to 0 for the deleted user.

A problem arises when you try to delete more than one user, where each user has voted in the same poll. The code below will try to set the UID to 0, however that tuple may already exist in the table, so the db_query will fail and report an error.

function poll_user($op, &$edit, &$user) {
  if ($op == 'delete') {
    db_query('UPDATE {poll_votes} SET uid = 0 WHERE uid = %d', $user->uid);
  }
}

How to reproduce:

1. Create at least two user accounts.

2. Create a poll, and have both user accounts vote in the poll.

3. Delete one user account; all should be fine when the "poll_user" function exeuctes.

4. Delete the other user account, and a "user warning: Duplicate entry ...." database error will occur.

Consequences:

The "user warning: Duplicate entry ...." database error will occur every time a new user is deleted (where they had previously voted in the same poll). Additionally, the deleted UID's will remain in the "{poll_votes}" table since they can't be removed.

Possible Solution:

Instead of trying to set the UID back to 0, it would be better to completely remove the user entry since, after all, the account is deleted. This will keep the {poll_votes} table clean.

Comments

sammo’s picture

+1

2 years on, no fix and no feedback :-(

You can override the submit function in your own module by doing something like this


function mymodule_form_poll_view_voting_alter(&$form, &$form_state) {
  $form['vote']['#submit'] = array('mymodule_poll_vote_sub');
}


function mymodule_poll_vote_sub($form, &$form_state = NULL) {
  $node = $form['#node'];
  $choice = $form_state['values']['choice'];

  global $user;
  if ($user->uid) {
    db_query("INSERT INTO {poll_votes} (nid, chorder, uid, hostname) VALUES (%d, %d, %d, '%s')", $node->nid, $choice, $user->uid, mt_rand());
  }

etc...

lang14’s picture

StatusFileSize
new2.24 KB

Created a patch that will run a schema update on the poll module that will require no modifications other than the update to the poll module.

Due to there already being a key, and I couldn't find a db_remove_key() in the API I had to do some things that were a little unorthodox but this was the simplest fix that would require little testing (in my mind!)

lang14’s picture

StatusFileSize
new2.77 KB

New patch based off of Drupal git repo (instead of my own)

sammo’s picture

Thanks for the patch. Lets see if it ends up in the core package?

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.