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
Which indexes did you define?
Which indexes did you define? Could you please post your schema declaration?
The table i use to insert the
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
If it's a new node, then
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.
Node nid
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.
Show us your whole function
Show us your whole function then.
Please see
Please see http://heine.familiedeelstra.com/coding-help-tips
<?phpfunction
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
At a short look, it looks
At a short look, it looks ok.
Going back to the original problem, how do you know the rows aren't saving?
I'm trying it on a node with
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.
Before your query, add
Before your query, add this:
And after your code, output this:
(if you are using mysql). Try to track down the problem. Then post the results here.
This gives interesting
This gives interesting results.
When i add:
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.
That means the value is being
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.
It's a very simple query to
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.
My suggestion. Why don't you
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.
It will never work for two
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.
In this case (after editting
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
The taxonomy module does it's
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.
Well that explains it then!
Well that explains it then!
Aah, Thank you so much ! I
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 !