使用drupal_write_record插入提交的表单内容DB [复制](Insert submi

2019-10-17 05:32发布

可能重复:
插入提交的表单内容使用drupal_write_record到DB

我在新的Drupal和我试图做的是我的第一个模块,所以希望你能理解我。 我创建了一个注册模块,并希望通过将其连接到数据库drupal_write_record ,但我不知道究竟该怎么做。 我读了如何使用Drupal的,从这个网站的方法,但仍无法做到这一点。

能否请你帮我解决的呢?

这里是我的模块怎么样子:

function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
    'title' => 'Contact Information',
    'description' => 'Contact Information',
    'page callback' => 'drupal_get_form',
    'access callback' => true,
    'weight' => '10',
    'page arguments' => array('my_module_form'),
    'access arguments' => array('access content'),
    'type' =>  MENU_NORMAL_ITEM,
);
$items['my_module/list/1'] = array(
    'title' => t('Contact list'),
    'page callback' => 'my_module_form_list_page',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK, 
);
return $items;
}
function my_module_perm() {
return array('access my_module content', 'access administration pages');
}

function my_module_help($path, $arg) {
$output = '';  //declare your output variable
switch ($path) {
case "admin/help#my_module":
    $output = '<p>'.  t("Displays information about site") .'</p>';
    break;
}
return $output;
}
function my_module_form($form_data) {
$form['contact_information'] = array(
    '#value' => variable_get('contact_form_information', t('Please fill in all fields.'))
);
$form['first'] = array(
    '#type' => 'textfield',
    '#title' => t('First name'),
    '#maxlength' => 20,
    '#required' => TRUE,
);
$form['last'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name'),
    '#maxlength' => 20,
    '#required' => TRUE,
);
$form['street'] = array(
    '#type' => 'textfield',
    '#title' => t('Street Address'),
    '#required' => TRUE,
); 
$form['city'] = array(
    '#type' => 'textfield',
    '#title' => t('City'),
    '#required' => TRUE,
); 
$form['postal'] = array(
    '#type' => 'textfield',
    '#title' => t('Postal Code'),
); 
$form['state'] = array(
    '#type' => 'select',
    '#title' => t('State'),
    '#default_value' => variable_get('feed_item_length','chisinau'),
    '#options' => array(
    'chisinau' => t('Chisinau'), 
    'balti' => t('Balti'), 
    'cahul' => t('Cahul')),
); 
$form['phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Business Phone'),
    '#required' => TRUE,
); 
$form['ext'] = array(
    '#type' => 'textfield',
    '#title' => t('ext'),
); 
$form['mobile'] = array(
    '#type' => 'textfield',
    '#title' => t('Mobile'),
    '#required' => TRUE,
); 
$form['home_phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Home Phone'),
);
$form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#required' => TRUE,
);
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
);
$form['clear'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#validate' => array('my_module_my_form_clear'),
);
return $form;
}

function my_module_form_submit($form, &$form_data){
global $user;
    $data = array(
        'id' => $user->id,
        'first' => $form_data['values']['first'],
        'last' => $form_data['values']['last'],
        'street' => $form_data['values']['street'],
        'city' => $form_data['values']['city'],
        'postal' => $form_data['values']['postal'],
        'state' => $form_data['values']['state'],
        'phone' => $form_data['values']['phone'],
        'ext' => $form_data['values']['ext'],
        'mobile' => $form_data['values']['mobile'],
        'home_phone' => $form_data['values']['home_phone'],
        'email' => $form_data['values']['email'],
    );
drupal_write_record('my_module', $data ,'id');
drupal_set_message(t('Information has been saved to the database')); 
}

使用这种方法的工作,但不是我所需要的:

function my_module_form_submit($form_id, &$form_data)  {
db_query("INSERT INTO my_module (id, first, last, street, city, postal, state, phone, ext, mobile, home_phone, email)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
$form_data['values']['first'],
$form_data['values']['last'], 
$form_data['values']['street'],
$form_data['values']['city'],
$form_data['values']['postal'], 
$form_data['values']['state'],
$form_data['values']['phone'],
$form_data['values']['ext'], 
$form_data['values']['mobile'],
$form_data['values']['home_phone'],
$form_data['values']['email']); 
drupal_set_message(t('Information has been saved to the database')); 
}

My_module.install文件:

function my_module_install() {
  drupal_install_schema('my_module');
}

function my_module_uninstall() {
 drupal_uninstall_schema('my_module');
  } 

function my_module_schema(){
  $schema = array();
  $schema['my_module'] = array(
  'description' => 'My Module table.',
  'fields'=>array(  
  'id'=>array(  
  'type'=>'serial',  
  'unsigned'=>TRUE,  
  'not null'=>TRUE  
  ),  
  'first'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),  
  'last'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ), 
  'street'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'city'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'postal'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'state'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'phone'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'ext'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ), 
  'mobile'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'home_phone'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  'email'=>array(  
  'type'=>'varchar',  
  'length'=>255,  
  'default' => 'NULL',  
  'null'=>TRUE  
  ),
  ),  
   'primary key'=>array("id"), 
  );
  return $schema;
  }

什么是错的,如何解决呢?

Answer 1:

的第三个参数drupal_write_record应该是主ID数组。 在你的情况(如果该字段ID是真正的主键。)应该是array('id') 然而,因为我不能看到整个hook_schema功能,我不能说,如果你有一个主键集?

您正在使用Drupal 7的Drupal的或6? Drupal的6还需要你有一个hook_install()与函数调用drupal_install_schema('my_module')

了解更多关于drupal_write_record这里: http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_write_record/7 。



文章来源: Insert submitted form content to DB using drupal_write_record [duplicate]