提交到数据库时表单值显示为空白 - Drupal FormAPI
过去一周半我一直在研究这个 drupal 表单 API 脚本。为了深入了解我的问题..下面的表格仅列出了一系列数据库记录,其中包含 5 个单独的评分等级。 (思想、行动、关系、语言和 IT)。
这段代码是我自己的自定义模块的一部分,其中所有值都从数据库中列出。该模块背后的想法是能够大规模编辑这些值。
我无法将表单中输入的值传递给 marli_admin_submit 函数内部的变量。第二个问题是将这些值分配给它们的特定 ID。为此,我想补充一点,我只是想更新一个分数,而不是全部。
下面是我的代码。
任何建议表示赞赏。
function marli_scores(){
$result = pager_query(db_rewrite_sql('SELECT * FROM marli WHERE value != " "'));
while ($node = db_fetch_object($result)) {
$attribute = $node->attribute;
$field = $node->field_name;
$item = $node->value;
$mind = $node->mind;
$action = $node->action;
$relationship = $node->relationship;
$language = $node->language;
$it = $node->it;
$form['field'][$node->marli_id] = array('#type' => 'markup', '#value' => $field, '#prefix' => '<b>', '#suffix' => '</b>');
$form['title'][$node->marli_id] = array('#type' => 'markup', '#value' => $item, '#prefix' => '<b>', '#suffix' => '</b>');
$form['mind'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $mind);
$form['action'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $action);
$form['relationship'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $relationship);
$form['language'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $language);
$form['it'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $it);
}
$form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
$form['save'] = array('#type' => 'submit', '#value' => t('Save'));
$form['#theme'] = 'marli_scores';
return $form;
}
function marli_admin_submit($form, &$form_state) {
$marli_id = 4;
$submit_mind = $form_state['values']['mind'][$marli_id];
$submit_action = $form_state['values']['action'][$marli_id];
$submit_relationship = $form_state['values']['relationship'][$marli_id];
$submit_language = $form_state['values']['language'][$marli_id];
$submit_it = $form_state['values']['it'][$marli_id];
$sql_query = "UPDATE {marli} SET mind = %d, action = %d, relationship = %d, language = %d, it = %d WHERE marli_id = %d";
if ($success = db_query($sql_query, $submit_mind, $submit_action, $submit_relationship, $submit_language, $submit_it)) {
drupal_set_message(t(' Values have been saved.'));
}
else {
drupal_set_message(t('There was an error saving your data. Please try again.'));
}
}
I have been working on this drupal form API script for past week and half. to give an insight into my problem.. the form below merely lists a host of database records which contain 5 individual scoring ranks. (mind, action, relationship, language and IT).
this code is apart of my own custom module where all values are listed from the database. the idea behind this module is to be able to edit these values on a large scale.
I am having trouble getting the values entered in the form to be passed to the variables inside of the marli_admin_submit function. the second problem is the assigning those values to their specific ID. for this purpose id like to add im merely trying to get just one score updated rather than all of them.
below is my code.
any advice appreciated.
function marli_scores(){
$result = pager_query(db_rewrite_sql('SELECT * FROM marli WHERE value != " "'));
while ($node = db_fetch_object($result)) {
$attribute = $node->attribute;
$field = $node->field_name;
$item = $node->value;
$mind = $node->mind;
$action = $node->action;
$relationship = $node->relationship;
$language = $node->language;
$it = $node->it;
$form['field'][$node->marli_id] = array('#type' => 'markup', '#value' => $field, '#prefix' => '<b>', '#suffix' => '</b>');
$form['title'][$node->marli_id] = array('#type' => 'markup', '#value' => $item, '#prefix' => '<b>', '#suffix' => '</b>');
$form['mind'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $mind);
$form['action'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $action);
$form['relationship'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $relationship);
$form['language'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $language);
$form['it'][$node->marli_id] = array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $it);
}
$form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
$form['save'] = array('#type' => 'submit', '#value' => t('Save'));
$form['#theme'] = 'marli_scores';
return $form;
}
function marli_admin_submit($form, &$form_state) {
$marli_id = 4;
$submit_mind = $form_state['values']['mind'][$marli_id];
$submit_action = $form_state['values']['action'][$marli_id];
$submit_relationship = $form_state['values']['relationship'][$marli_id];
$submit_language = $form_state['values']['language'][$marli_id];
$submit_it = $form_state['values']['it'][$marli_id];
$sql_query = "UPDATE {marli} SET mind = %d, action = %d, relationship = %d, language = %d, it = %d WHERE marli_id = %d";
if ($success = db_query($sql_query, $submit_mind, $submit_action, $submit_relationship, $submit_language, $submit_it)) {
drupal_set_message(t(' Values have been saved.'));
}
else {
drupal_set_message(t('There was an error saving your data. Please try again.'));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我声明
#value 需要是 #default_value ..否则它会继续覆盖。
The problem was where i declared
the #value needs to be #default_value.. otherwise it keeps overriding.