跳转到主要内容
鱼鱼 提交于 20 July 2011

以下是我一个模块的.module文件,在该文件中我实现了表单的呈现,然后向将表单输进去的内容存进数据库中absent表中,具体代码如下:

<?php // $Id$ /** * @file * Play with the Form API. */ /** * Implementation of hook_menu(). */ function absence_menu() { $items['absence'] = array( 'title' => 'View the form', 'page callback' => 'absence_page', 'access arguments' => array('access content'), ); return $items; } /** * Menu callback. * Called when user goes to http://example.com/?q=absence */ function absence_page() { $output = t('This page contains our absence information.'); // Return the HTML generated from the $form data structure. $output .= drupal_get_form('absence_date'); return $output; } /** * Define a form. */ function absence_date($form_state,$node) { $form['Date'] = array( '#title' => t('Date'), '#type' => 'textfield', '#size' => '12', '#description' => t('Please enter date.'), ); $form['Absence'] = array( '#title' => t('Absence'), '#type' => 'textfield', '#size' => '12', '#description' => t('Fill the total hours you be absent.'), ); $form['Code'] = array( '#type' => 'radios', '#title' => t('Code'), '#description' => t('A:Sick B:Excused C:Vacation D:Annual E:Unexcused F:Others.'), '#options' => array( t('A'), t('B'), t('C'), t('D'), t('E'), t('F') ), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit') ); return $form; }

function absence_date_submit($form, $form_state) { global $user; $date = $form_state['values']['Date']; $absence = $form_state['values']['Absence']; $code = $form_state['values']['Code']; db_query("INSERT INTO {absent} (name, Date, Absence, Code) VALUES (%s, %d, '%d', %s)", $user->name,$date, $absence, $code); drupal_set_message(t('Your absence record has been saved.'));}

但是当我提交表单后,数据并没有被存进数据库中,且有如下提示 * warning: Missing argument 2 for absence_date() in D:\wamp\www\timesheet1\sites\all\modules\custom\absence\absence.module on line 32. * user warning: Unknown column 'sophia' in 'field list' query: INSERT INTO absent (name, Date, Absence, Code) VALUES (sophia, 2011, '4', 0) in D:\wamp\www\timesheet1\sites\all\modules\custom\absence\absence.module on line 75. * warning: Missing argument 2 for absence_date() in D:\wamp\www\timesheet1\sites\all\modules\custom\absence\absence.module on line 32.