I keep getting the error "SQLSTATE[HY093]: Invalid parameter number: parameter was not defined" when using the UPDATE function in my php, even when my blindValues match the statement variables - I can't see where the error is in my syntax. Any help would be much appreciated as am new to php and this is part of an assessment. Thank you!
if (isset($_POST["Update"])) {
updateTEAM($_POST["ID"]);
}
function updateTEAM($id) {
try {
$conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0908659_OFA", $GLOBALS['username'], $GLOBALS['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conn->prepare("UPDATE TEAM SET ID=:id, TeamName=:teamname, Logo=:logo WHERE ID=". $id);
$statement->bindValue(":id", $_POST["ID"]);
$statement->bindValue(":teamname", $_POST["TeamName"]);
$statement->bindValue(":logo", $_POST["Logo"]);
$result = $statement->execute();
if ($result) {
$GLOBALS['message'] = "Team record was updated";
} else {
$GLOBALS['message'] = "The Team record was not updated";
}
}
catch(PDOException $e) {
echo "A problem occured: " . $e->getMessage();
}
$conn = null;
}
I can't reproduce the error, but I reproduced your file and database and created a supposed POST. I updated with success !.
I believe this is some incorrect information coming from your POST
My file .php
<?php
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);
$_POST["ID"] = "123";
$_POST["TeamName"] = "Palmeiras";
$_POST["Logo"] = "palmeiras.jpg";
updateTEAM('12');
function updateTEAM($id) {
try {
$conn = new PDO("mysql:host=localhost;dbname=e0908659_OFA", 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conn->prepare("UPDATE TEAM SET ID=:id, TeamName=:teamname, Logo=:logo WHERE ID=". $id);
$statement->bindValue(":id", $_POST["ID"]);
$statement->bindValue(":teamname", $_POST["TeamName"]);
$statement->bindValue(":logo", $_POST["Logo"]);
$result = $statement->execute();
if ($result) {
echo "Team record was updated";
} else {
echo "The Team record was not updated";
}
}
catch(PDOException $e) {
echo "A problem occured: " . $e->getMessage();
}
$conn = null;
}
And my sql
CREATE TABLE `team` (
`ID` INT(11) NULL DEFAULT NULL,
`TeamName` VARCHAR(50) NULL DEFAULT NULL,
`Logo` VARCHAR(50) NULL DEFAULT NULL
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
INSERT INTO team
(ID
, TeamName
, Logo
) VALUES (12, 'Corinthians', 'corinthians.jpg');