为什么mysql表中的name列最终为空而没有插入名称?

时间:2012-12-16 01:03:43

标签: php mysql forms drupal-6

当我运行此代码时,会创建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.'));

}
?>

1 个答案:

答案 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.'));

}