Can anyone provide a fresh set of eyes on this piece of code. Basically, $user->uid is not set for some reason in the first if statement, however it works fine in the other two.
function user2userpoints_giveform_validate($form_id, $form_values) {
global $user;
if (!empty($form_values['to'])) {
$to = user_load(array('name' => $form_values['to']));
if ($to->uid) {
$responses = db_fetch_array(db_query("SELECT * FROM {userpoints_txn} WHERE uid=$to->uid AND approver_uid=$user->uid AND reference='done'"));
if($responses) {
form_set_error('to',t('You have already picked a winner for this fight.'));
drupal_goto($_SERVER['HTTP_REFERER']);
}
}
/* Check the name to be valid. */
if (!$to->uid) {
form_set_error('to', t('That is not a valid user.'));
}
if ($to->uid == $user->uid) {
form_set_error('to', t("You can't declare yourself the winner."));
}
}
I'm entering a username in a form and comparing it to the currently logged in user who submitted the form. The first if statement doesn't trip the error, even though it should, while the last two do. Most puzzling is that $user->uid in the last if statement is set correctly, but it isn't in the first one. If I take the AND approver_uid=$user->uid out of the Select statement, it works fine. (But of course I need that condition in there.)
I've been staring at this way too long, hoping fresh eyes can pick out my mistake. Thanks.