I am trying to add some functionality to the organic groups module. I wish to be able to add or remove users to og's from the user edit pages. I have created a form using the below code which shows which groups the user is a member of however I am stuck when trying to get the values from the check boxes and adding the values to the database.

This is the code that displays the checkboxes with appropriate names and displays whether the user is currently a member. I have tried a few different values for the return value (to update the database in different ways).

case 'form':
$result_og = db_query("SELECT title, nid FROM node WHERE type = 'og'");
if (user_access('administer organic groups')) {
while ($field = db_fetch_object($result_og)) {
if (!isset($fields['ogGroups'])) {
$fields['ogGroups'] = array('#type' => 'fieldset', '#title' => ogGroups, '#weight' => $w++);
}
$result_user = db_query("SELECT nid FROM og_uid WHERE $account->uid = uid && $field->nid = nid");
$chkbx_checked = 0;
while ($field_user = db_fetch_object($result_user)) {
if ($field_user->nid == $field->nid) {
$chkbx_checked = 1;
break;
}
}
$fields['ogGroups'][$field->title] = array('#type' => 'checkbox',
'#title' => $field->title,
'#default_value' => $chkbx_checked,
'#return_value' => 1,
);
}
}
return $fields;
break;

Below is my update code. I have borrowed from the profile.module. at present it will clear all entries for the user being edited but will then add them to og in the database rather than the groups that are selected by the checkboxes. can anyone give me help/pointers as to how to fix this?

case 'update':
$result = db_query("SELECT title, nid FROM node WHERE type = 'og'");
while ($field = db_fetch_object($result)) {
if (_og_field_serialize($field->title)) {
$edit['ogGroups'] = serialize($edit['ogGroups']);
}
db_query("DELETE FROM og_uid WHERE uid = %d AND nid = %d", $account->uid, $field->nid);
db_query("INSERT INTO og_uid (nid, og_role, is_active, uid) VALUES (%d, %d, %d, %d)", $field->nid, '0', $edit[$ogGroups][$field->title->return_value], $account->uid);
$edit['ogGroups'] = NULL;
}
break;

Bype