Drupal hook_submit写入db [英] Drupal hook_submit write to db

查看:134
本文介绍了Drupal hook_submit写入db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在承担构建自定义模块的挑战。



我已经开始非常小,只是为了尝试和了解drupal钩子和模块。 p>

我创建了一个带有一个文本条目的简单表单,唯一的验证是该字段不为空。



在表单提交我想写一个自定义表。表格存在。

我的字段是:

nid int(11)

eid int(11)主键自动增量

title varchar(50)



这是我的表单:

 函数my_module_my_form($ form_state){
$ form ['esp'] = array(
'#type'=>'fieldset',
'#title'=> t一个ESP'),
'#collapsible'=> TRUE,
'#collapsed'=> FALSE,

);
$ form ['esp'] ['title'] = array(
'#type'=>'textfield',
'#title'=> t('ESP Name '),
'#required'=> TRUE,
'#default_value'=>'',
'#description'=>输入ESP名称,
'#size'=> 20,
'#maxlength'=> 20,
);

$ form ['submit'] = array(
'#type'=>'submit',
'#value'=> t('Submit' ,
);
return $ form;
}

这里是 hook_submit()我已经设置了:

  function my_module_my_form_submit($ form,$ form_state){
db_query INSERT INTO my_module_esp(title)VALUES(。$ form_state ['values'] ['title']。));
drupal_set_message(t('表单已提交。));
}

但这不工作...任何建议?
此外,我想添加最新的nid到这个表,我如何获取该值并更新它在数据库中,所以它不会影响任何其他模块?



任何帮助将不胜感激。

解决方案

您需要考虑表单的层次结构。尝试使用:

  function my_module_my_form_submit($ form,$ form_state){
db_query('INSERT INTO {my_module_esp} title)VALUES(%s)',$ form_state ['values'] ['esp'] ['title']);
drupal_set_message(t('表单已提交。));
}

如果你需要指定一个nid,那么你应该实际创建一个节点在你的代码,所以301是真的相关的东西。节点表中的主键是auto_increment,所以如果你不在这段代码中创建一个节点,那么别人可以去创建一个博客文章,并且将被赋予301.使用 node_save()创建节点


I am now taking on the challenge of building a custom module.

I have started really small just to try and the understanding of drupal hooks and modules.

I have created a simple form with one text entry, the only validation is that the field is not empty.

On the form submit I would like to write to a custom table. The table exists.
My fields are:
nid int(11)
eid int(11) Primary Key Auto Increment
title varchar(50)

Here is my form:

function my_module_my_form($form_state) {
$form['esp'] = array(
  '#type' => 'fieldset',
  '#title' => t('Add a ESP'),
  '#collapsible' => TRUE,
  '#collapsed' => FALSE,

);
$form['esp']['title'] = array(
  '#type' => 'textfield',
  '#title' => t('ESP Name'),
  '#required' => TRUE,
  '#default_value' => '',
  '#description' => "Enter the ESP Name",
  '#size' => 20,
  '#maxlength' => 20,
);

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Submit'),
);
return $form;
 }

and here is the hook_submit() that I have setup:

function my_module_my_form_submit($form, $form_state) {
db_query("INSERT INTO my_module_esp (title) VALUES (".$form_state['values']['title'].")");
drupal_set_message(t('The form has been submitted.'));
}

But this does not work... any suggestions? Also I would like the add the latest nid to this table, how would I get that value and update it in the db so it does not affect any other modules?

Any help would be much appreciated

解决方案

You need to take the form's hierarchy into account. Try using:

function my_module_my_form_submit($form, $form_state) {
  db_query('INSERT INTO {my_module_esp} (title) VALUES ("%s")', $form_state['values']['esp']['title']);
  drupal_set_message(t('The form has been submitted.'));
}

If you need to to assign this a nid, then you should actually create a node in your code so that 301 is really associated with something. The primary key in the node table is auto_increment, so if you don't create a node in this code, then someone else could go create a blog post and would get assigned 301. Use node_save() to create a node

这篇关于Drupal hook_submit写入db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆