[drupal-devel] [bug] possible logic error in taxonomy_node_save

killes drupal-devel at drupal.org
Mon Mar 14 19:35:40 UTC 2005

Issue status update for http://drupal.org/node/18861

 Project:      Drupal
 Version:      cvs
 Component:    taxonomy.module
 Category:     bug reports
 Priority:     normal
 Assigned to:  Anonymous
 Reported by:  nkurz
 Updated by:   killes at www.drop.org
-Status:       active
+Status:       patch

Well, spotted. I don't think that the current code will corrupt the
database, but it should get fixed nevertheless.

killes at www.drop.org

Previous comments:

March 14, 2005 - 03:34 : nkurz

I'm brand new to Drupal and really rusty with PHP, but I just did a CVS
checkout and was looking through taxonomy.module wondering how I could
go about adding weights to taxonomy terms
(http://drupal.org/node/18824) and think I've found a small logic error
in taxonomy_node_save().  Or else I'm misunderstanding some really
simple PHP.  In any case, it seemed worth reporting so that someone
familiar with the module could at least take a quick look.

function taxonomy_node_save($nid, $terms) {
  if (is_array($terms)) {
    foreach ($terms as $term) {
      if (is_array($term)) {
        foreach ($term as $tid) {
          if ($tid) {
            db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d,
%d)', $nid, $tid);
      if ($term) {
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)',
$nid, $term);

Walkthrough: taxonomy_node_save is passed an integer $nid and an array
$terms.  Each array element in $terms  is either an array of tid's or a
single integer tid.  If the element is an array of tid's, we insert each
(nid, tid) pair into {term_node}.  But then after doing this, it looks
like we also try to insert (nid,ARRAY) into {term_node} after we finish
the foreach loop.
Is this perhaps supposed to be 'else if ($term)'?  Or is 'if(ARRAY)'
false in PHP?  Apologies for making a bad first impression if I'm just
dead wrong here.


March 14, 2005 - 03:47 : nkurz

Attachment: http://drupal.org/files/issues/taxonomy_node_save.patch (504 bytes)

Here's the trivial untested patch in case that makes anything clearer.

More information about the drupal-devel mailing list