Function to update SQL is not working. I'm working with user cake

So I am using UserCake as a base for a project that I am working on. So far, I have successfully added fields to the registration form such as First Name, Last Name, Company, Address, etc. I am now attempting to create the PHP update functions so the user can edit their information.

UserCake had an updateEmail() function already, which works like a charm. I decided to use that function as template to update the First Name, however it is not working.

=================update_settings.php=================
[php]
if ($email != $loggedInUser->email) {
if(trim($email) == “”) {
$errors[] = lang(“ACCOUNT_SPECIFY_EMAIL”);
} else if (!isValidEmail($email)) {
$errors[] = lang(“ACCOUNT_INVALID_EMAIL”);
} else if (emailExists($email)) {
$errors[] = lang(“ACCOUNT_EMAIL_IN_USE”, array($email));
}

	//End data validation
	if(count($errors) == 0) {
		$loggedInUser->updateEmail($email);
		$successes[] = lang("ACCOUNT_EMAIL_UPDATED");
	}
}

if ($first_name != $loggedInUser->first_name) {
	if(trim($first_name) == "") {
		$errors[] = lang("ACCOUNT_SPECIFY_FNAME");
	}
	
	//End data validation
	if(count($errors) == 0) {
		$loggedInUser->updateFirstName($first_name);
		$successes[] = lang("ACCOUNT_FNAME_UPDATED");
	}
}

[/php]

=================funcs.php=================
[php]
//Update a user’s email
function updateEmail($id, $email) {
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("UPDATE ".$db_table_prefix.“users
SET
email = ?
WHERE
id = ?”);
$stmt->bind_param(“si”, $email, $id);
$result = $stmt->execute();
$stmt->close();
return $result;
}

//Change a user’s first name
function updateFirstName($id, $first_name) {
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("UPDATE ".$db_table_prefix.“users
SET
first_name = ?
WHERE
id = ?”);
$stmt->bind_param(“si”, $first_name, $id);
$result = $stmt->execute();
$stmt->close();
return $result;
}
[/php]

The code stops when I call updateFirstName(); leaving a blank page. updateFirstName()returns a 1 (same as the updateEmail() ) but does not continue to the next line! I have searched high and low for a solution but cannot seem to find one.

Here’s my table:

CREATE TABLE `uc_users` (
`id` int(11) NOT NULL,
  `password` varchar(225) NOT NULL,
  `email` varchar(150) NOT NULL,
  `activation_token` varchar(225) NOT NULL,
  `last_activation_request` int(11) NOT NULL,
  `lost_password_request` tinyint(1) NOT NULL,
  `active` tinyint(1) NOT NULL,
  `title` varchar(150) NOT NULL,
  `sign_up_stamp` int(11) NOT NULL,
  `last_sign_in_stamp` int(11) NOT NULL,
  `company` varchar(50) DEFAULT NULL,
  `address_1` varchar(50) NOT NULL,
  `address_2` varchar(50) DEFAULT NULL,
  `city` varchar(50) NOT NULL,
  `state` varchar(20) NOT NULL,
  `zip` int(5) NOT NULL,
  `paid` tinyint(1) NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `last_name` varchar(50) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

Any help is appreciated!

when you call the function here, you are providing one argument ($first_name)
[php]
$loggedInUser->updateFirstName($first_name);
[/php]

However, the function requires two arguments ($id, $first_name).

[php]
//Change a user’s first name
function updateFirstName($id, $first_name) {
}
[/php]

This will cause an error and the script to fail.
To fix, you will need to either
a: supply the $id when calling the function.
b: make $id=false in the when defining the function.
If you choose option b, switch the args around so as not to have a false value first.

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service