我在一个自定义模块中创建了一个表单,在一个 drupal 7 项目中,我需要将值插入到一个名为“玩家”的自定义内容类型中
这是我的表格:
function custom_module_reg_form($form, $form_state){
$form['first_name'] = array(
'#type' => 'textfield',
'#attributes' => array('placeholder' => t('First Name')),
);
$form['last_name'] = array(
'#type' => 'textfield',
'#attributes' => array('placeholder' => t('Last Name')),
);
$form['email_address'] = array(
'#type' => 'textfield',
'#attributes' => array('placeholder' => t('Email Address')),
);
$form['state'] = array(
'#type' => 'textfield',
'#attributes' => array('placeholder' => t('State')),
);
$form['zip_code'] = array(
'#type' => 'textfield',
'#attributes' => array('placeholder' => t('Zip Code')),
);
$form['phone_number'] = array(
'#type' => 'textfield',
'#attributes' => array('placeholder' => t('Phone Number')),
);
$form['password'] = array(
'#type' => 'password',
'#attributes' => array('placeholder' => t('Password')),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Register',
);
return $form;
}
这是提交功能,但出现错误:
function custom_module_reg_form_submit($form, $form_state){
$first_name = $form_state['values']['first_name'];
$last_name = $form_state['values']['last_name'];
$email_address = $form_state['values']['email_address'];
$state = $form_state['values']['state'];
$zip_code = $form_state['values']['zip_code'];
$phone_number = $form_state['values']['phone_number'];
$password = encrypt($form_state['values']['password']);
$nid = db_insert('players')->fields(array(
'first_name' => $first_name,
'last_name' => $last_name,
'email_address' => $email_address,
'state' => $state,
'zip_code' => $zip_code,
'phone_number' => $phone_number,
'password' => $password,
'created' => REQUEST_TIME
))->execute();
// Save new node
$node = new stdClass();
// Set node title
$node->title = $email_address;
// set node type ex: article etc
$node->type = "players";
// set node language
$node->language = LANGUAGE_NONE;
//(1 or 0): published or not
$node->status = 0;
//(1 or 0): promoted to front page
$node->promote = 0;
node_object_prepare($node);
node_save($node);
}
我正在关注一个示例,我在日志中看到该表不正确,但我找不到其他任何给出示例的地方。我究竟做错了什么?为以编程方式从表单插入创建自定义表是否更好?谢谢,请告诉我。