tags:

views:

225

answers:

1

I needed to create a custom select list for the user registration page that pulls a SQL query from nodes I need to link to users. I have successfully accomplished this task.. :)

However, when I submit the value, I can't seem to control where the value is stored. In fact, I can't store the value at all. I have created a custom field for my value, but only the new field name is stored, and it is serialized and stored in the Data column of the user table.

Below is my code, I've commented my issues in it. Any help would be appreciated!

 <?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');
      }
A: 

Have you checked Drupal's logs? It should be throwing errors, as this is not a valid query.

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

Should be:

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

Additionally, in:

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

$form_id is never defined.

You say you've created the field in the database, but it must match the name of the Drupal field to be automatically handled. You've got two different names for it - account_name in the Drupal field, but account in the database. Make them consistent and it should be automatically handled, no submit functions required.

ceejayoz
I don't see anything in the logs. I changed the query, but same results. I'm not sure it's even getting to the query. Is there something wrong with the function that calls the query??
cinqoTimo
You are correct. It is now passing the selected index (good enough!) of the select list.Sorry, I am new to drupal, and it is powerful, but driving me a little crazy...
cinqoTimo

related questions