I wrote the following custom PHP code to create a node

<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

global $user;
$title1=$node->title;
$node = new stdClass;
$node->type = 'Campaign_PlayerList';
$node->title = $title1;
$node->uid = $user->uid;
$node->language='und';
$node->field_cmp_playerid['und'][0]['value']=$user->uid;
$node->field_cmp_userscore['und'][0]['value']=1;
$node->field_cmp_usersupport['und'][0]['value']=1;
node_object_prepare($node);
node_save($node);
?>

The values for field_cmp_playerid and field_cmp_attend_time are not saved.

$node->field_cmp_playerid['und'][0]['value']=$user->uid;

doesnt work and field_cmp_attend_time is the same too.

What could the problem be?

Here is a screenshot of the field management form for the Campaign Playerlist content type:

screenshot

share|improve this question

1 Answer

up vote 1 down vote accepted

It looks from your image like field_cmp_playerid is a user reference field, which means the column name won't be value, it will be uid. Try this code:

$node->field_cmp_playerid[LANGUAGE_NONE][0]['uid'] = $user->uid;

The field_cmp_attend_time field probably isn't getting saved because it's missing from your code. It's just a date field so the column name will be value. If the date field you're using is a UNIX timestamp type, for example, the code would look like this:

$node-> field_cmp_playerid[LANGUAGE_NONE][0]['value'] = REQUEST_TIME;

Just in case you want to update your field_cmp_id as well, it's a node reference so the column name becomes nid:

$node->field_cmp_id[LANGUAGE_NONE][0]['nid'] = $nid;
share|improve this answer
Thank you Clive! And Thx for helping me revise my question. – cobenash Jun 8 '12 at 9:03

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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