Drupal 6:将自定义字段导入数据库 [英] Drupal 6: Getting custom fields into the database

查看:91
本文介绍了Drupal 6:将自定义字段导入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个无法由核心配置文件模块(从SQL查询填充的选择列表)提供的自定义字段。我能够成功地添加字段与适当的选项;然而,我不确定如何处理这个新的领域一旦提交。



从我的理解,我需要编写一个处理我的SQL插入的函数,然后调用函数从一个hook_form_alter提交按钮。



到目前为止,它只传递字段名称,而不是值。并且字段名称被序列化并存储在用户表的数据字段中。我试图将它传递给自己的列。



这是我的代码...

  //获取值并将其插入到帐户字段
函数accountselect_submitaccount(){
db_query(INSERT INTO {user}(account)
VALUES {account_name} );
}

然后...



pre> //使用自定义提交调用上述函数(我怀疑这是麻烦的区域)
函数accountselect_form_alter(& $ form,$ form_state,$ form_id){
if($ form_id =='user-register')
$ form ['#submit'] [] ='accountselect_submitaccount';
}


解决方案

据我所知,仅添加到数据列中,如果没有与字段名匹配的列。您应该能够在用户表中创建一个帐户列,并填写该数据。



对于您发布的代码,您的提交函数有许多问题:




  • 提交函数的功能包括提交的值。 submit_function($ form,& $ form_state)可以访问 $ form_state ['values'] 数组。

  • INSERT 永远无法使用。您需要一个更新查询。

  • 您不能执行 {account_name} 在查询中。您需要实际使用 $ form_state ['values'] 中的值。


I needed a custom field that couldn't be provided by the core profile module (Select list populated from a SQL query). I was able to successfully add the field with proper options; however, I am unsure how to handle this new field once submitted.

From what I understand, I need to write a function that handles my SQL insert, and then call that function from a hook_form_alter submit button.

As of now, it is passing only the field name, not the value. And the field name is being serialized and stored in the 'data' field of the user table. I'm attempting to pass it to its own column.

Here is my code...

//takes value and inserts it to account field   
 function accountselect_submitaccount() {
        db_query( "INSERT INTO {user} (account)
                  VALUES {account_name}" );
      }

Then...

//call the above function using custom submit (I suspect this is the troubled area)
  function accountselect_form_alter(&$form, $form_state, $form_id) {
    if($form_id == 'user-register')
    $form['#submit'][] = 'accountselect_submitaccount';
  }

解决方案

To my knowledge, new fields are only added to the data column if there's no column matching the field name. You should be able to just create a column of account into the user table and it'll get populated with that data.

As for the code you've posted, your submission function has a number of issues:

  • Submit functions take functions that include the submitted values. submit_function($form, &$form_state) gives you access to the $form_state['values'] array.
  • An INSERT will never work. You need an UPDATE query.
  • You can't do {account_name} in the query. You need to actually use the value from $form_state['values'].

这篇关于Drupal 6:将自定义字段导入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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