提交到数据库时,表单值显示为空白 - Drupal FormAPI

时间:2010-04-27 10:31:38

标签: php drupal form-submit drupal-fapi

过去一周半,我一直在研究这个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.'));
  }

}

1 个答案:

答案 0 :(得分:0)

问题出在我宣布的地方

 $form['mind'][$node->marli_id] =  array('#type' => 'textfield', '#maxlength' => '1', '#size' => '1', '#value' => $mind);

#value需要是#default_value ..否则它会继续覆盖。