tags:

views:

392

answers:

4

Hey,

I have been searching for how to create nodes in Drupal 6. I found some entries here on stackoverflow, but the questions seemed to either be for older versions or the solutions did not work for me. Ok, so here is my current process for trying to create

$node = new stdClass();

$node->title = "test title";
$node->body = "test body";
$node->type= "story";
$node->created = time();
$node->changed = $node->created;
$node->status = 1;
$node->promote = 1;
$node->sticky = 0;
$node->format = 1;
$node->uid = 1;

node_save( $node );

When I execute this code, the node is created, but when I got the administration page, it throws the following errors:

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

user warning: Duplicate entry '36' for key 1 query: INSERT INTO node_comment_statistics (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (36, 1269980590, NULL, 1, 0) in C:\wamp\www\steelylib\sites\all\modules\nodecomment\nodecomment.module on line 409.

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

warning: Invalid argument supplied for foreach() in C:\wamp\www\steelylib\includes\menu.inc on line 258.

I've looked at different tutorials, and all seem to follow the same process. I'm not sure what I am doing wrong. I am using Drupal 6.15. When I roll back the database (to right before I made the changes) the errors are gone. Any help is appreciated!

Edit:

After playing around with it a bit, I did find that I had an error in my 'access arguments' in my hook_menu(), but as far as the duplicate entry goes, I was never able to figure it out. So, if you run into the same problem, good luck and post on here if you find the problem! :-)

+1  A: 

You need to wipe out the node, node revision, and node comment statistics table.

The problem is it is trying to insert a record that already exists in node comment statistics.

Kevin
i thought that too, but the node that already exists is the one that i just inserted (when inspecting it in the database). It's almost as if it is getting inserted twice.
John
Well, node_save will invoke the hook_nodeapi save operation in any other enabled modules. Start by disabling non essential modules.
Kevin
good thinking, i will do that tomorrow when i'm back at work.
John
Well, I tried emptying all of the tables, but still got the same error for the duplicate entry. I however fixed the other errors. See Farzan's post.
John
+1  A: 

I believe that the problem stems from somewhere else. Code snippet above is 100% correct. But I am sure you have a mistake somewhere.

I have encountered warnings in line 258 of menu.inc. Origin of warning was wrong menu entries. check all hook_menus in your module.
One common mistake -like mine- is assigning wrong values to these menu entries: 'access callback', 'access arguments', 'page callback', 'page arguments'

Keep these items in mind:

  • 'access arguments' and 'page arguments' must be arrays.
  • If you want to grant unlimited access to a menu entry do like this: 'access callback' => true

Regarding the Duplicate entry, I still have no idea.

farzan
Thank you! you were right on the hook_menu. I had access arguments as a single value instead of an array containing only one value. However, that only solved the menu.inc problems. I'm still receiving that troubling entry already exists message.
John
A: 

I'm not sure what's going on with your site exactly, would need check your db and other stuff, but the error you are seeing is cause by this line:

db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, %d)', $node->nid, $node->created, $node->uid, 0);

It is exacuted when a node is saved and everything looks fine. It's the place where something get's inserted into that table. Somehow though, you already have an entry for the node with nid 36 in your node_comment_statistics table. I don't know if your tables are out of sync, or you are inserting two rows into this table.

Possible reasons:

  • You have some custom code / other module that uses this table?
  • You are triggering the nook_nodeapi op insert twice in your code when a node is created.
googletorp
I'm definitely inserting two rows into the table. The tables are in perfect sync. It is trying to insert two duplicate rows. I'm looking to try to find the problem still...
John
A: 

What I have done to programatically create node in Drupal 6 is;

$node = new stdClass();

$node->name    = "test title";
$node->title   = $node->name;
$node->body    = "test body";
$node->type    = "story";
$node->created = time();
$node->changed = $node->created;
$node->status  = 1;
$node->promote = 1;
$node->sticky  = 0;
$node->format  = 1;
$node->uid     = 1;

if ($node = node_submit($node)) {
  node_save($node);
}
else {
  // Process error
}
wilsonodk