Module 使用drupal\u write\u记录将提交的表单内容插入数据库

Module 使用drupal\u write\u记录将提交的表单内容插入数据库,module,form-api,database,Module,Form Api,Database,可能重复: 我是drupal新手,我想做的是我的第一个模块,希望你们能理解我。 我创建了一个注册模块,并希望通过将其连接到数据库,但我不知道具体怎么做。我在drupal网站上读到了如何使用这种方法,但还是做不到 你能帮我处理一下吗 以下是我的模块的外观: function my_module_menu() { $items = array(); $items['my_module/form'] = array( 'title' => 'Contact Information',

可能重复:

我是drupal新手,我想做的是我的第一个模块,希望你们能理解我。 我创建了一个注册模块,并希望通过将其连接到数据库,但我不知道具体怎么做。我在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')); 
}
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;
  }

有什么问题以及如何解决?

drupal\u write\u record的第三个参数应该是主ID的数组。在您的情况下(如果字段id确实是主键。)应该是
array('id')
。但是,因为我看不到整个hook_模式函数,所以我不能说您是否有主键集

您使用的是Drupal7还是Drupal6?Drupal 6还要求您有一个
hook\u install()
,带有对
Drupal\u install\u模式(“我的模块”)的函数调用。


阅读更多关于drupal_write_record的信息:。

drupal_write_record的第三个参数应该是主ID的数组。在您的情况下(如果字段id确实是主键。)应该是
array('id')
。但是,因为我看不到整个hook_模式函数,所以我不能说您是否有主键集

您使用的是Drupal7还是Drupal6?Drupal 6还要求您有一个
hook\u install()
,带有对
Drupal\u install\u模式(“我的模块”)的函数调用。


阅读更多关于drupal_write_record的信息:。

我删除了一个提交按钮,现在只剩下一个,点击后,我收到确认消息:“信息已保存到数据库”。你能在这里指导我如何使用wathdog方法吗?提前感谢。此消息是仅在删除其中一个提交按钮后显示,还是在之前显示?现在,
my_module
表中是否插入了任何记录?watchdog只用于登录数据库,而不是在屏幕上显示drupal\u set\u message之类的消息您在更新还是插入?我在表单数组中看不到
'id'=>$user->id,
,它是表的主键吗?如果是这种情况,那么在调用drupal\u write\u record时需要使用第三个参数。您是否创建了表
my_module
,它是否包含您的所有字段?编辑:我对StackExchange是个新手。我是否应该将此作为评论添加到帖子中?因为这不是一个真正的答案。“drupal\u set\u message”以前也不高兴。我的_模块现在有一条记录,我用刚才升级的方法添加它。我正在尝试向数据库添加(插入)记录。请检查上面我的my_module.install文件的外观。在“my_UModule”表中,我的第一行是自动递增IDI DELETE一个submit按钮,现在我只剩下一个,在我点击它之后,我收到确认消息:“信息已保存到数据库中”。你能在这里指导我如何使用wathdog方法吗?提前感谢。此消息是仅在删除其中一个提交按钮后显示,还是在之前显示?现在,
my_module
表中是否插入了任何记录?watchdog只用于登录数据库,而不是在屏幕上显示drupal\u set\u message之类的消息您在更新还是插入?我在表单数组中看不到
'id'=>$user->id,
,它是表的主键吗?如果是这种情况,那么在调用drupal\u write\u record时需要使用第三个参数。您是否创建了表
my_module
,它是否包含您的所有字段?编辑:我对StackExchange是个新手。我是否应该将此作为评论添加到帖子中?因为这不是一个真正的答案。“drupal\u set\u message”以前也不高兴。我的_模块现在有一条记录,我用刚才升级的方法添加它。我正在尝试向数据库添加(插入)记录。请检查上面我的my_module.install文件的外观。在“my_module”表中,我的第一行是使用drupal 6自动递增IDI,上面我刚刚更新了我的完整模式方法。我再次启动,从数据库中删除了表并禁用了模块。之后,我启用了我的模块,但
my_module.install
没有在db中创建表。我试图理解错误在哪里。我使用Drupal6,上面我刚刚更新了我的完整模式方法。我再次启动,从数据库中删除了表并禁用了模块。之后,我启用了我的模块,但
my_module.install
没有在db中创建表。我正在努力弄清楚哪里出了错。
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;
  }