2009-12-16 87 views
0

我需要爲用戶註冊頁面創建一個自定義選擇列表,該列表從需要鏈接到用戶的節點提取SQL查詢。我已經成功地完成了這項任務.. :)Drupal:在註冊時修改用戶

但是,當我提交的值,我似乎無法控制存儲值的位置。事實上,我根本無法存儲這個值。我爲我的值創建了一個自定義字段,但只存儲新的字段名稱,並將其序列化並存儲在用戶表的Data列中。

下面是我的代碼,我已經評論了我的問題。任何幫助,將不勝感激!

<?php 
    // $Id$ 

     //create the additional user form field, a select list named "account_name" 
     //then calls the next function to populate it. 
     function accountselect_user($op, &$edit, &$account, $category = NULL) { 
     if ($op == 'register' || 'edit') 
     $fields['Information']['account_name'] = array(
     '#type' => 'select', 
     '#title' => 'Account', 
     '#description' => t('Select the account to which the contact belongs'), 
     '#options' => accountselect_getclubs() , 
     ); 
     return $fields; 
     } 

     //contains query to pull results to select list...this part is working 
     function accountselect_getclubs() { 
      $return = array(); 
      $sql = 'SELECT DISTINCT `title` FROM node WHERE type = \'accounts\' '; 
      $result = db_query($sql); 
     while ($row = db_fetch_array($result)) { 
      $return[] = $row['title']; 
     } 

     return $return; 
     } 

     //CAN'T GET THIS PART TO WORK - query to update the row - the uid = 29 is for 
     //testing puposes. Once I get the test value to work 
     //the test value I will worry about updating the correct user. 
     function accountselect_submitaccount() { 
     $sql = "UPDATE users SET account = \'value\' WHERE uid = \'29\'"; 
     db_query($sql); 
     drupal_set_message(t('The field has been updated.')); 
     } 

     //I SUSPECT THE PROBLEM IS HERE...call the submitaccount function. 
     //I have tried hook_form_alter as well... 
     function accountselect_submit(&$form, &$form_state) { 
     if($form_id == 'user-register') 
     drupal_execute('accountselect_submitaccount'); 
     } 

回答

0

您是否檢查過Drupal的日誌?它應該拋出錯誤,因爲這不是一個有效的查詢。

$sql = "UPDATE users SET account = \'value\' WHERE uid = \'29\'"; 

應該是:

$sql = "UPDATE users SET account = 'value' WHERE uid = '29'"; 

此外,在:

function accountselect_submit(&$form, &$form_state) { 
    if($form_id == 'user-register') 
    drupal_execute('accountselect_submitaccount'); 
} 

$form_id從未定義。

你說你已經在數據庫中創建了該字段,但它必須匹配要自動處理的Drupal字段的名稱。您有兩個不同的名稱 - Drupal字段中的account_name,但數據庫中的account。使它們一致並且應該自動處理,不需要提交功能。

+0

我在日誌中看不到任何東西。我改變了查詢,但結果相同。我不確定它甚至可以查詢。調用查詢的函數有問題嗎? – tpow 2009-12-16 16:21:06

+0

你是對的。它現在傳遞選擇列表的選定索引(足夠好!)。 對不起,我是drupal的新手,它功能強大,但讓我有點瘋狂...... – tpow 2009-12-16 16:29:18