2012-12-16 54 views
0

當我運行這段代碼時,創建了mysql數據庫表student_log中的一行。但是,student_name列未填充用戶輸入的值。這是怎麼回事?爲什麼mysql表中的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

你應該看$ form_state [ '值'],就像這樣:

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

} 
+0

爲什麼會[ '值']?謝謝 – ingrid

+0

bc:'#default_value'=> $ form_state ['values'] ['name']? – ingrid

+0

這裏有一個很好的解釋$ form_state的地方:http://drupal.org/node/751826他們幾乎從不提及任何東西,只是$ form_state ['values'],但form_state中還有其他的數組元素,例如。 $ form_state [ '重定向'。 – sonofagun

相關問題