0

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

1 Answer 1

1

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;
1
  • Thank you Clive! And Thx for helping me revise my question. Commented Jun 8, 2012 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.