当我运行此代码时,会创建mysql数据库表student_log中的一行。但是,student_name列未填充用户输入的值。这是怎么回事?
<?php
function student_grant_menu() {
$items = array();
$items['student_grant/form'] = array(
'title' => t('student_grant'),
'page callback' => 'student_grant',
'access arguments' => array('access content'),
'description' => t('student_grant'),
'type' => MENU_CALLBACK,
);
return $items;
}
function student_grant_form() {
return drupal_get_form('student_grant_my_form');
}
function student_grant_my_form($form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 30,
'#required' => TRUE,
);
// Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
// name of table is student_log and field is student_name. Name passed from form_state is put in student_name field?
function student_grant_my_form_submit($form, &$form_state) {
db_query("INSERT INTO {student_log} (student_name) VALUES ('%s')",
$form_state['name']);
drupal_set_message(t('Your form has been saved.'));
}
?>
答案 0 :(得分:0)
您应该查看$ form_state ['values'],如下所示:
function student_grant_my_form_submit($form, &$form_state) {
db_query("INSERT INTO {student_log} (student_name) VALUES ('%s')",
$form_state['values']['name']);
drupal_set_message(t('Your form has been saved.'));
}