I have a INSERT query that doesn't execute. I'm pretty sure i call it the right way, but it's not actually executed.

The query looks like this:

db_query("INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)", $node->nid, $node->vid, $result->tid);

I've also tried:

db_query("INSERT INTO {term_node} VALUES ('%d', '%d', '%d')", $node->nid, $node->vid, $result->tid);
db_query("INSERT INTO {term_node} (nid, vid, tid) VALUES ('".$node->nid."', '".$node->vid."', '".$result->tid)."' ");

$fields = array("nid" => $node->nid, "vid" => $node->vid, "tid" =>$result->tid);
drupal_write_record("term_node",$fields);

( I've doublechecked all arguments and they all are integer values )
Nothing seems to work. I did however saw something strange when trying the INSERT query with some hard coded values. Drupal returned a "User warning: Duplicate entry ...".
So it seems like drupal does read the db_query right, but just not executes it.

I'm calling db_query() in hook_nodeapi with $op='presave'.

Who can help me ?

Comments

dani.latorre’s picture

Which indexes did you define? Could you please post your schema declaration?

jan_v’s picture

The table i use to insert the values is term_node (taxonomy module table). I didn't alter this table in any way.

It can't be a index issue because if i output the db_query string and execute it in my query browser, the insert works fine

Jaypan’s picture

If it's a new node, then $node->nid is not set in 'presave'. The NID has to be unique, but you will be trying to insert zero for NID on each insert, so the insert will fail.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

jan_v’s picture

I am aware of that, but the hook_nodeapi also passes the presave operation when the node was editted.
So here the nid does have a value, but the INSERT still fails.

I've doublechecked all of the query arguments, and none of them are empty. They are all integer values.

Jaypan’s picture

Show us your whole function then.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

jan_v’s picture

<?php
function imagebody_nodeapi(&$node,$op,$a3 = null, $a4 = null) {
    switch(
$op){
        case
'presave':
           
$tags = $node->taxonomy;
            if(
variable_get('imagebody_inherit_tags',FALSE) && empty($tags['tags']['2']) && $node->nid != ''){
               
$queryResult = db_query("SELECT tid from {image_attach} inner join {term_node} on image_attach.nid = term_node.nid where iid = '%s'", $node->nid);
                while(
$result = db_fetch_object($queryResult)){
                   
//$fields = array("nid" => $node->nid, "vid" => $node->vid, "tid" =>$result->tid);
                    //drupal_write_record("term_node",$fields);
                    
db_query("INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)", $node->nid, $node->vid, $result->tid);
                }
            }else{
               
drupal_set_message(t('failed'));
            }
        break;
      }
}
?>

For a long time i thought it was because some arguments might be empty, but i've checked on this very often and they are all integers. Drupal returns no messages

Jaypan’s picture

At a short look, it looks ok.

Going back to the original problem, how do you know the rows aren't saving?

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

jan_v’s picture

I'm trying it on a node with a certain nid, when i execute the query "SELECT * FROM term_node WHERE nid = 'certain nid';" in my query browser i get nada.

Jaypan’s picture

Before your query, add this:

<?php
echo $node->nid, ' ', $node->vid, ' ', $result->tid, '<br />';
?>

And after your code, output this:

<?php
die(mysql_last_error());
?>

(if you are using mysql). Try to track down the problem. Then post the results here.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

jan_v’s picture

This gives interesting results.

When i add:

<?php
die(mysql_last_error());
?>

after my db_query, i get a blank screen BUT there's a record inserted in the database, and when i remove the die(mysql_last_error()); and refresh the page, i get a drupal error message saying there's a duplicate entry for that record.

So when i run the code, but die right after the db_query the code gets inserted (twice?) and when i run the code without the die(), nothing is inserted.

Jaypan’s picture

That means the value is being inserted the first time. Your query to retrieve it must be wrong. Look directly at your database using phpmyadmin or something.

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

jan_v’s picture

It's a very simple query to recieve : "select * from term_node where nid = 9114" when i just editted node/9114 . The query returns nothing and the node 9114 still has no taxonomy tags.

dani.latorre’s picture

My suggestion. Why don't you just modify the node attributes in presave. Presave is called after node passes validation and before insert or update. So you don't need execute an insert in presave, just modify the node tid.

nevets’s picture

It will never work for two reasons: 1) If the node is new, nid will not be set (the nodes never been saved) and 2) because of the way the taxonomy module works. Since you module should run before the 'taxonomy' module you should be able to use the insert and/or update operations and add to the values in $node->taxonomy.

jan_v’s picture

In this case (after editting a node) nid isn't empty. None of my arguments are empty.

Your second point seems like a good solution. But i'm still curious why the db_query won't work here.

Thanks

nevets’s picture

The taxonomy module does it's thing by first deleting all terms for the node (good-bye to your additions) and then creating new entries for any terms still used by the node.

Jaypan’s picture

Well that explains it then!

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

jan_v’s picture

Aah, Thank you so much ! I didn't know that about the taxonomy module. Probably would take me weeks to figure that out : )

Thanks to all for the quick responses. Great help !