Drupal 6:只能将第一个字符的值插入到MySQL中 [英] Drupal 6: Only inserting first character of value to MySQL
问题描述
一切都很好。我可以看到,我希望的值显示在HTML源代码中,所以我知道我的查询正在执行。但是,当我提交表单时,它只会插入该值的第一个字符。我的几个测试值分别为566,784,1004,列值分别为5,7,1。
起初我以为它必须是DB列的属性,但是当我移除了使字段隐藏的form_alter并手动选择该值时,插入了正确的值?$?
<?php
function addSR_form_service_request_node_form_alter(& $ form,$ form_state){
if(arg(0)=='user'&&& is_numeric(arg(1))){
$ account = arg(1);
$ club = 2589;
$ form ['field_sr_account'] = array('#type'=>'hidden',
'#value'=> $ club
);
}
}
?>
任何人都可以看到为什么只能插入第一个字符?
注意:我尝试删除并重新创建列,使用#value& #default_value,它仍然只提交整数的第一个字符。此外,我删除了提交处理程序作为一个可能的原因删除它,仍然导致只有一个字符被提交
更多更新 - 仍然搜索!
好的,有一些很好的问题。允许我回答他们:
- DB列类型是整数(4)
-
钩子产生的HTML是:
input type =hiddenname =field_sr_accountid =edit-field-sr-accountvalue =2589
最新更新:我认为问题已经缩小到数组的结构。当我在表单alter处理完毕后,在这个字段上执行var_dump,这就是我所得到的。
[43] = > Array
(
[#type] => hidden
[#default_value] => 2589
[#post] =>数组
(
)
[#programmed] =>
[#tree] =>
[#parents] =>数组
(
[ 0] => field_sr_account
)
[#array_parents] =>数组
(
[0] => field_sr_account
)
[#weight] => 0.016
[#processed] => 1
[#description] =>
[#attributes] =& b $ b(
)
[#required] =>
[#input] => 1
[#process] =>数组
(
[0] => form_expand_ahah
)
[#nam e] => field_sr_account
[#id] => edit-field-sr-account
[#value] => 2589
[#defaults_loaded] => 1
[#sorted] => 1
)
我可以将表单值设置为。它必须是abhaga的建议。
由于您尝试更改的字段最初是使用一个选择的小部件CCK将寻找 $ form_state ['values'] ['field_sr_account'] [0] ['value']
。通过将字段设置为#hidden类型并设置#value,您将在 $ form_state ['values'] ['field_sr_account']
中获取其值。 CCK将尝试访问其中的第一个元素,并以该值的第一个字符结尾。
已更新:实现最简单的方法你需要做些什么:
function addSR_form_service_request_node_form_alter(& $ form,$ form_state){
if (arg(0)=='user'&&& is_numeric(arg(1))){
$ account = arg(1);
$ club = 2589;
//使用此属性存储要还原的值
$ form ['#field_sr_account'] = $ club;
$ form ['field_sr_account'] = array('#type'=>'hidden','#value'=> $ club);
}
}
/ *在您的提交处理程序中,以正确的格式恢复值* /
$ form_state ['values'] ['field_sr_account'] = array('0'=> array('value'=> $ form ['#field_sr_account']));
旧答案
一种实现你的
尝试做的一种方式是将整个
$ code $ $ form ['field_sr_account'] 复制到
$ form ['#field_sr_account']
然后
通过
提交的价值通过SQL
查询以正确的格式在
提交处理程序本身。
I am working with a hook_form_alter on a CCK type (for you drupal-ers). I have a field that is normally a select list in my node form. However, in this instance, I want to hide the select list, and populate its value in the form with an SQL query.
Everything was going nicely. I could see that my desired value was showing up in the HTML source, so I knew my query was executing properly. However, when I submit the form, it only inserts the first character of the value. A few of my tests were values of 566, 784, 1004 - the column values were 5,7,1, respectively.
At first I thought it had to be the DB column attributes, but when I removed my form_alter that makes the field hidden and select the value manually, the correct value is inserted?!?
<?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$account = arg(1);
$club = 2589;
$form['field_sr_account'] = array( '#type' => 'hidden',
'#value' => $club
);
}
}
?>
Can anyone see why only the first character would be inserted??
Note: I have tried deleting and recreating the column, using #value & #default_value, and it is still submitting only the first character of the integer. Also, I eliminated the submit handler as a possible cause by removing it, which still resulted in only one character being submitted
More Updates - Still Searching! Okay, some good questions. Allow me to answer them:
- The DB column type is integer(4)
The HTML the hook produces is :
input type="hidden" name="field_sr_account" id="edit-field-sr-account" value="2589"
Latest Update: I think the issue has been narrowed to the structure of the array. When I do var_dump on this field after the form alter has been processed, this is what I get..
[43] => Array
(
[#type] => hidden
[#default_value] => 2589
[#post] => Array
(
)
[#programmed] =>
[#tree] =>
[#parents] => Array
(
[0] => field_sr_account
)
[#array_parents] => Array
(
[0] => field_sr_account
)
[#weight] => 0.016
[#processed] => 1
[#description] =>
[#attributes] => Array
(
)
[#required] =>
[#input] => 1
[#process] => Array
(
[0] => form_expand_ahah
)
[#name] => field_sr_account
[#id] => edit-field-sr-account
[#value] => 2589
[#defaults_loaded] => 1
[#sorted] => 1
)
What is the structure of the field that I can set the form value to. It's gotta be something like what abhaga is suggesting..
Since the field you are trying to change was originally using a select widget, CCK will be looking for $form_state['values']['field_sr_account'][0]['value']
. By setting the field to a #hidden type and setting #value, you will get its value in $form_state['values']['field_sr_account']
. CCK will try to access the first element of that and end up with the first character of the value.
Updated: The easiest way to achieve what you need would be to do something:
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$account = arg(1);
$club = 2589;
// Use this property to store the value to restore back
$form['#field_sr_account'] = $club;
$form['field_sr_account'] = array( '#type' => 'hidden','#value' => $club);
}
}
/*in your submit handler, restore the value in the proper format*/
$form_state['values']['field_sr_account'] = array('0' => array('value' => $form['#field_sr_account']));
Old Answer
One way of accomplishing what you are trying to do is to copy the whole
$form['field_sr_account']
into$form['#field_sr_account']
and then provide the value through the SQL query in the right format in the submit handler itself.
这篇关于Drupal 6:只能将第一个字符的值插入到MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!