I am trying to UPDATE table with a prepared statement, for some reason I have a hard time doing it as I keep getting an error with the statement. I am using prepared statements often and not sure what I am missing.
That is what I got (part of a function):
$result = array();
try {
$sql = "UPDATE UsersData SET firstName = :firstName, lastName = :lastName, birthDay = :birthDay, aboutMe = :aboutMe, facebookU = :facebookU, twitterU = :twitter, githubU = :github WHERE uId =: id";
$stmt = $this->connect()->prepare($sql);
$stmt->execute(array(
':firstName' => $userInformation['firstName'],
':lastName' => $userInformation['lastName'],
':birthDay' => $userInformation['birthDay'],
':aboutMe' => $userInformation['aboutMe'],
':facebookU' => $userInformation['facebook'],
':twitterU' => $userInformation['twitter'],
':github' => $userInformation['github'],
':id' => $userInformation['id']
));
$result = true;
} catch (\PDOException $e) {
$result = "Error: " . $e->getMessage();
}
return $result;
I get
Fatal error: Uncaught Error: Call to a member function execute() on bool in
On line
$stmt->execute(array(
I tried as well:
$result = array();
try {
$sql = "UPDATE UsersData AS UD, Users AS US
SET
UD.firstName=:firstName,
UD.lastName=:lastName,
UD.birthDay=:birthDay,
UD.aboutMe=:aboutMe,
UD.facebookU=:facebookU,
UD.twitterU=:twitter,
UD.githubU=:github
US.uEmail=:email
WHERE UD.uId=:id AND US.id=:id";
$stmt = $this->connect()->prepare($sql);
$stmt->execute(array(
':firstName' => $userInformation['firstName'],
':lastName' => $userInformation['lastName'],
':birthDay' => $userInformation['birthDay'],
':aboutMe' => $userInformation['aboutMe'],
':facebookU' => $userInformation['facebook'],
':twitterU' => $userInformation['twitter'],
':github' => $userInformation['github'],
':id' => $userInformation['id']
));
$result = true;
} catch (\PDOException $e) {
$result = "Error: " . $e->getMessage();
}
return $result;
Got the same error.
I found a solution but I am not sure why it would work like that and not the other ways:
$sql = "UPDATE UsersData SET firstName =?, lastName =?, birthDay =?, aboutMe =?, facebookU =?, twitterU =?, githubU =? WHERE uId = ?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([
$userInformation['firstName'],
$userInformation['lastName'],
$userInformation['birthDay'],
$userInformation['aboutMe'],
$userInformation['facebook'],
$userInformation['twitter'],
$userInformation['github'],
$userInformation['id']
]);
Thank in advance!
– Apr 19 '20 at 07:55