Prepared statements getting 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'

I have put together three complicated prepared INSERTS now all of a sudden cannot get two simple ones to work.

$sql = "INSERT INTO randomTime (
transmissionProgramID,
numberOfTimes,
minimumGap,
randomTimeType,
lastUpdate
) VALUES (
:transmissionProgramID,
:numberOfTimes,
:minimumGap,
:randomTimeType,
:lastUpdate
);";
$statement = $pdo->prepare($sql);

var_dump($transmissionProgramID,$numberOfTimes,$randomTimeType, $minimumGap,$lastUpdate);

$statement->bindValue(':transmissionProgramID,', $transmissionProgramID);
$statement->bindValue(':numberOfTimes,', $numberOfTimes);
$statement->bindValue(':minimumGap,', $minimumGap);
$statement->bindValue(':randomTimeType,', $randomTimeType);
$statement->bindValue(':lastUpdate', $lastUpdate);
$result = $statement->execute();

… and I get "Parameter not defined

Cannot seem to add a picture but the table is short so…

CREATE TABLE `randomTime` (
`randomTimeID` INT(11) NOT NULL AUTO_INCREMENT,
`numberOfTimes` TINYINT(4) NOT NULL DEFAULT '1',
`minimumGap` SMALLINT(6) NOT NULL DEFAULT '60',
`randomTimeType` ENUM('average','fixed','maximum') NULL DEFAULT 'fixed',
`transmissionProgramID` INT(11) NOT NULL,
`lastUpdate` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`randomTimeID`),
UNIQUE INDEX `randomTimeID` (`randomTimeID`),
INDEX `FK_randomTime_transmissionProgram` (`transmissionProgramID`),
CONSTRAINT `FK_randomTime_transmissionProgram` FOREIGN KEY (`transmissionProgramID`) REFERENCES `transmissionProgram` (`transmissionProgramID`)
)

The var_dump produces what it should eg:

string(3) "131" string(1) "2" string(7) "average" int(200) string(19) "2016-03-01 14:36:42" 

I tried an intval on the numbers but that made no different and had not used intval on the ones that worked.

Grateful for any help. Two hours and counting. And going bonkers not seeing what I have done wrong.

Assuming that minimumGap is meant to be the integer in th var_dump, try:

$statement->bindParam(‘:minimumGap’, $minimumGap, PDO::PARAM_INT);

By default PDO treats bound parameters as strings

Isn’t the problem on these lines:

$statement->bindValue(':transmissionProgramID,', $transmissionProgramID);
$statement->bindValue(':numberOfTimes,', $numberOfTimes);
$statement->bindValue(':minimumGap,', $minimumGap);
$statement->bindValue(':randomTimeType,', $randomTimeType);

But not on this line:

$statement->bindValue(':lastUpdate', $lastUpdate);

That is, you’ve included a comma inside the quotes where you specify the parameter names on the first four lines, but not on the final line? Or doesn’t that matter?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.