2

I've created a custom module in D7, wich gothers info from user via form.api. Next, I've created a Custom Content Type 'questions' where I setup fields according to my form.

How do I save data to the CCK?

Note: I've tryed db_insert(), but it refers only to 'node' which nsert one or more records to the node table.

    $nid = db_insert('node') // Table name no longer needs {}
->fields(array(
  'title' => 'Example',
  'uid' => 1,
  'created' => REQUEST_TIME,
))
->execute();
3

You should never insert into the node (or any other entity table) directly, there's a fully-fledged API for handling such things. At the moment you're bypassing a lot of the core goodness that the node module offers, and you're likely to run into problems (you're not updating the revision table for example).

node_save() is the best tool for the job here:

$node = new stdClass;
$node->type = 'the_type';
$node->language = LANGUAGE_NONE;

node_object_prepare($node);

// Add a field
$node->field_some_field[$node->language][0]['value'] = 'Some value';

// Save the node
node_save($node);

// The node is saved by this point so the nid is available.
$nid = $node->nid;
  • Thanks, for prompt responce. I am feeling so stupid because of searching at wrong direction:) – Jim Nov 9 '12 at 10:51
  • 2
    @Jim, the documentation for this is really scattered. Once you start getting familiar with the terminology, you can target searches better. – mpdonadio Nov 9 '12 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.