我正在构建一个drupal站点,我希望管理员能够将目标输入到drupal数据库的表中,然后可以输出给其他用户完成。
创建输入和表格的表单,但是在按下提交时,值不会添加到表格中,我不确定原因。
我从我连接到我的网站的模块中获得以下代码。我认为问题发生在我创建字段的顶部,或者在我提交值时在底部。我真的不确定。
谁能告诉我哪里出错了以及如何解决?
<?php
function achievementList_init() {
if(!(db_table_exists('achievements'))) {
$achvmnt_list_schema = array(
'description' => 'Achievement Fields',
'fields' => array (
'achvmntID' => array('type' => 'int', 'unsigned' => TRUE, 'AUTO_INCREMENT' => TRUE, 'not null' => TRUE),
'achvmntName' => array('type' => 'varchar', 'length' => 256, 'not null' => TRUE, 'default' => ''),
'achvmntDesc' => array('type' => 'varchar', 'length' => 256, 'not null' => TRUE, 'default' => ''),
'achvmntPts' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
));
db_create_table('achievements', $achvmnt_list_schema);
}
}
function achievementList_menu($form, &$form_state) {
$items = array();
$items['achievementList/addAchievement'] = array(
'title' => 'Achievement Input',
'description' => 'Input Achievement info to add achievement to database',
'page callback' =>'drupal_get_form',
'page arguments' => array('achievementList_form'),
'access callback' => TRUE
);
return $items;
}
function achievementList_form($form, &$form_state) {
$form['name'] = array (
'#type' => 'textfield',
'#title' => 'Achievement Name',
'#size' => '20',
'#maxlength' => '20',
'#required' => TRUE
);
$form['description'] = array(
'#type' => 'textfield',
'#title' => 'Achievement Description',
'#size' => 50,
'#maxLength' => 400,
'#required' => TRUE
);
$form['points'] = array(
'#type' => 'textfield',
'#title' => 'Point Award',
'#size' => '20',
'#maxlength' => '20',
'#required' => TRUE
);
$form['add_button'] = array(
'#type' => 'submit',
'#value' => t('Add Achievement')
);
return $form;
}
function achievementList_validate($form, &$form_state) {
$name = $form_state['values']['name'];
if($form_state['values']['name'] = '') {
form_set_error('name', t('Achievement must have a name!'));
}
if($form_state['values']['description'] = '') {
form_set_error('name', t('Please describe the achievement!'));
}
if(!($form_state['values']['points'] > 0)) {
form_set_error('name', t('Achievement must be worth points!'));
}
}
function achievementList_submit($form, &$form_state) {
db_insert('achievements')
->fields(array(
'achvmntName' => $form_state['values']['name'],
'achvmntDesc' => $form_state['values']['description'],
'achvmntPts' => $form_state['values']['points'],
))
->execute();
}
答案 0 :(得分:1)
干杯!!!