MyBB Community Forums

Full Version: Getting user's uid function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I know this is a common question, but none of the other answers have worked for me.

I have a function in datahandlers/user.php which is accessed from the user CP. Users input field data via a mod I made based on the default custom profile field functionality. Similar to how the table _profilefields has fid(n) which inserts into _userfields which has ufid(n); _characterfields has cfid(n) and inserts into _characters with primary key of cid(n).

However, _userfields has a blank row for each user at user creation, which is when modified later. I want my _characters table to insert the user's uid into a column called ucid, so that a single user can have multiple cid's attached to them.

My current function works to insert the field data, but doesn't insert the user's UID.

function insert_character()
	{
		
		global $db, $plugins, $cache, $mybb;
		
		// Yes, validating is required.
		if(!$this->get_validated())
		{
			die("The user needs to be validated before inserting it into the DB.");
		}
		if(count($this->get_errors()) > 0)
		{
			die("The user is not valid.");
		}

		$user = &$this->data;

		if(is_array($user['usercharacter_fields']))
		{
			$query = $db->simple_select("characters", "*");
			$fields = $db->fetch_array($query);
			if(!$fields['ucid'])
			{
				$usercharacter_fields = array(
					'ucid' => $userid 
				);

				$fields_array = $db->show_fields_from("characters");
				foreach($fields_array as $field)
				{
					if($field['Field'] == 'ucid')
					{
						continue;
					}
					$usercharacter_fields[$field['Field']] = '';
				}
				$db->insert_query("characters", $user['usercharacter_fields'], "ucid='{$user['uid']}'", false);
			}
		}
		
	}

How do I populate the $user variable with uid? It keeps coming up as NULL. Any help would be greatly appreciated. Cheers!
$mybb->user['uid'];
$usercharacter_fields = array(
      'ucid' => $mybb->user['uid'] 
);
I've tried that before, still comes up as null...
^ this might sound silly, have you included global.php
usercp.php has global, I threw require_once "./global.php" at the top of user.php as well, but to no avail... do I need to put it somewhere else?
^ IIRC, you need to add it at top of your plugin file as it requires MyBB variables.
There is no plugin file--I used the word plugin to mean customization--sorry. The function in question is in user.php.


Edit: I thought maybe I've broken the $user in the usercp.php...?

if($mybb->input['action'] == "do_addcharacter" && $mybb->request_method == "post")
{
	// Verify incoming POST request
	verify_post_check($mybb->input['my_post_key']);

	$plugins->run_hooks("usercp_do_profile_start");

	// Set up user handler.
	require_once "inc/datahandlers/user.php";
	$userhandler = new UserDataHandler("update");

	$user = array(
		"character_fields" => $mybb->input['character_fields'],
		"ucid" => $mybb->user['uid'],
	);

	$userhandler->set_data($user);

	if(!$userhandler->validate_character())
	{
		$errors = $userhandler->get_friendly_errors();
		$errors = inline_error($errors);
		$mybb->input['action'] = "addcharacter";
	}
	else
	{
		$userhandler->insert_character();				
			
		$plugins->run_hooks("usercp_do_profile_end");
		redirect("usercp.php", $lang->redirect_characteradded);
	}
}

 
if($mybb->input['action'] == "addcharacter")
{
	$plugins->run_hooks("usercp_profile_start");

	$altbg = "trow1";
	$requiredfields2 = '';
	$customfields2 = '';
	$query = $db->simple_select("characterfields", "*", "editable=1", array('order_by' => 'disporder'));
	while($characterfield = $db->fetch_array($query))
	{
		// Does this field have a minimum post count?
		if($characterfield['postnum'] && $characterfield['postnum'] > $user['postnum'])
		{
			continue;
		}

		$characterfield['type'] = htmlspecialchars_uni($characterfield['type']);
		$characterfield['name'] = htmlspecialchars_uni($characterfield['name']);
		$characterfield['description'] = htmlspecialchars_uni($characterfield['description']);
		$thing = explode("\n", $characterfield['type'], "2");
		$type = $thing[0];
		$options = $thing[1];
		$field = "cfid{$characterfield['cfid']}";
		$select = '';
		if($errors)
		{
			$usercharacterfield = $mybb->input['character_fields'][$field];
		}
		else
		{
			$usercharacterfield = $user[$field];
		}
		if($type == "multiselect")
		{
			if($errors)
			{
				$useropts = $usercharacterfield;
			}
			else
			{
				$useropts = explode("\n", $usercharacterfield);
			}
			if(is_array($useropts))
			{
				foreach($useropts as $key => $val)
				{
					$val = htmlspecialchars_uni($val);
					$seloptions[$val] = $val;
				}
			}
			$expoptions = explode("\n", $options);
			if(is_array($expoptions))
			{
				foreach($expoptions as $key => $val)
				{
					$val = trim($val);
					$val = str_replace("\n", "\\n", $val);

					$sel = "";
					if($val == $seloptions[$val])
					{
						$sel = " selected=\"selected\"";
					}
					$select .= "<option value=\"$val\"$sel>$val</option>\n";
				}
				if(!$characterfield['length'])
				{
					$characterfield['length'] = 3;
				}
				$code = "<select name=\"character_fields[$field][]\" size=\"{$characterfield['length']}\" multiple=\"multiple\">$select</select>";
			}
		}
		elseif($type == "select")
		{
			$expoptions = explode("\n", $options);
			if(is_array($expoptions))
			{
				foreach($expoptions as $key => $val)
				{
					$val = trim($val);
					$val = str_replace("\n", "\\n", $val);
					$sel = "";
					if($val == htmlspecialchars_uni($usercharacterfield))
					{
						$sel = " selected=\"selected\"";
					}
					$select .= "<option value=\"$val\"$sel>$val</option>";
				}
				if(!$characterfield['length'])
				{
					$characterfield['length'] = 1;
				}
				$code = "<select name=\"character_fields[$field]\" size=\"{$characterfield['length']}\">$select</select>";
			}
		}
		elseif($type == "radio")
		{
			$expoptions = explode("\n", $options);
			if(is_array($expoptions))
			{
				foreach($expoptions as $key => $val)
				{
					$checked = "";
					if($val == $usercharacterfield)
					{
						$checked = " checked=\"checked\"";
					}
					$code .= "<input type=\"radio\" class=\"radio\" name=\"character_fields[$field]\" value=\"$val\"$checked /> <span class=\"smalltext\">$val</span><br />";
				}
			}
		}
		elseif($type == "checkbox")
		{
			if($errors)
			{
				$useropts = $usercharacterfield;
			}
			else
			{
				$useropts = explode("\n", $usercharacterfield);
			}
			if(is_array($useropts))
			{
				foreach($useropts as $key => $val)
				{
					$seloptions[$val] = $val;
				}
			}
			$expoptions = explode("\n", $options);
			if(is_array($expoptions))
			{
				foreach($expoptions as $key => $val)
				{
					$checked = "";
					if($val == $seloptions[$val])
					{
						$checked = " checked=\"checked\"";
					}
					$code .= "<input type=\"checkbox\" class=\"checkbox\" name=\"character_fields[$field][]\" value=\"$val\"$checked /> <span class=\"smalltext\">$val</span><br />";
				}
			}
		}
		elseif($type == "textarea")
		{
			$value = htmlspecialchars_uni($usercharacterfield);
			$code = "<textarea name=\"character_fields[$field]\" rows=\"6\" cols=\"30\" style=\"width: 95%\">$value</textarea>";
		}
		else
		{
			$value = htmlspecialchars_uni($usercharacterfield);
			$maxlength = "";
			if($characterfield['maxlength'] > 0)
			{
				$maxlength = " maxlength=\"{$characterfield['maxlength']}\"";
			}
			$code = "<input type=\"text\" name=\"character_fields[$field]\" class=\"textbox\" size=\"{$characterfield['length']}\"{$maxlength} value=\"$value\" />";
		}
		if($characterfield['required'] == 1)
		{
			eval("\$requiredfields2 .= \"".$templates->get("usercp_character_customfield")."\";");
		}
		else
		{
			eval("\$customfields2 .= \"".$templates->get("usercp_character_customfield")."\";");
		}
		$altbg = alt_trow();
		$code = "";
		$select = "";
		$val = "";
		$options = "";
		$expoptions = "";
		$useropts = "";
		$seloptions = "";
	}
	if($customfields2)
	{
		eval("\$customfields2 = \"".$templates->get("usercp_character_profilefields")."\";");
	}


	if($mybb->request_method == "post")
	{
		if(!trim($mybb->input["cfid{$characterfield['cfid']}"]))
		{
			$errors[] = "Error, missing field! D:";
		}
	}
		
		
		
	$plugins->run_hooks("usercp_profile_end");
	
	eval("\$editprofile = \"".$templates->get("usercp_addcharacter")."\";");
	output_page($editprofile);
}

Above you can see the usercp action, and the action executed when the button is pushed. Is this affecting my $user?

EDIT2: I tried making a separate function in user.php which looks like this:

	 function userid() {
	 	global $db, $plugins, $cache, $mybb;
		$userid=$mybb->user[uid];
		echo $userid;
	 }

This returned the uid. So there must be something wrong inside my function. Sad