Warning: Duplicate entry | Ad Module | Advertisements | Drupal 6

The Problem:

In the Drupal 6 Advertisements Module, Version 6.x-2.2 I was having the following error every time I resaved an ad or made changes to an ad:

user warning: Duplicate entry '32-0' for key 'PRIMARY' query: INSERT INTO ad_priority (aid, priority) VALUES(32, 0) in /data/www/flash_guru.localhost/sites/all/modules/ad/channel/ad_channel.module on line 710.

And

user warning: Duplicate entry '32-0' for key 'PRIMARY' query: INSERT INTO ad_channel_remnant (aid, remnant) VALUES(32, 0) in /data/www/flash_guru.localhost/sites/all/modules/ad/channel/ad_channel.module on line 715.

The Solution:

in modules/ad/channel/ad_channel.module I found that in the function _ad_channel_save_node they were not using a very thurough way of checking if the records already existed. I could have come up with something a little more efficient but I just through together a redundant solution. I changed the function from the following:

function _ad_channel_save_node($node) {
  // delete old channel information, then add new
  db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid);
  $channels = _ad_channel_get_enabled($node);
  foreach ($channels as $chid) {
    db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid);
  }
  if (user_access('configure ad premier status')) {
    db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid);
    if (!db_affected_rows()) {
      db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0);
    }
  }
    db_query('UPDATE {ad_channel_remnant} SET remnant = %d WHERE aid = %d', isset($node->remnant) ? $node->remnant : 0, $node->nid);
    if (!db_affected_rows()) {
      db_query('INSERT INTO {ad_channel_remnant} (aid, remnant) VALUES(%d, %d)', $node->nid, isset($node->remnant) ? $node->remnant : 0);
    }
}

 

to the following :

 

 
function _ad_channel_save_node($node) {
  // delete old channel information, then add new
  db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid);
  $channels = _ad_channel_get_enabled($node);
  foreach ($channels as $chid) {
    db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid);
  }
  if (user_access('configure ad premier status')) {
    db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid);
    if (!db_affected_rows()) {
 $isExists = FALSE;
 $result = db_query('SELECT priority FROM {ad_priority} WHERE aid = %d',$node->nid);
 if ($result){
 if (db_fetch_object($result)){
   $isExists = TRUE;
 }
 }
 if (!$isExists){
 db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0);
 }
    }
  }
    db_query('UPDATE {ad_channel_remnant} SET remnant = %d WHERE aid = %d', isset($node->remnant) ? $node->remnant : 0, $node->nid);
    if (!db_affected_rows()) {
 $isExists = FALSE;
 $result = db_query('SELECT remnant FROM {ad_channel_remnant} WHERE aid = %d',$node->nid);
 if ($result){
 if (db_fetch_object($result)){
   $isExists = TRUE;
 }
 }
 if (!$isExists){
       db_query('INSERT INTO {ad_channel_remnant} (aid, remnant) VALUES(%d, %d)', $node->nid, isset($node->remnant) ? $node->remnant : 0);
 }
    }
}

 

 

 

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security image
Write the displayed characters


busy