phpdrupaldrupal-nodes

Creating nodes programmatically in Drupal 6


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.

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.


Solution

  • 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:

    Regarding the Duplicate entry, I still have no idea.