user3080562 user3080562 - 4 months ago 11
SQL Question

PHP PDO Error - : Invalid parameter number

I'm trying to insert into a database for the first time using PDO but I keep getting the error

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' on line 25


Google tells me there is a mis match somewhere with the values I'm inserting but from what I can tell they add up

$db = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $db->prepare("INSERT INTO ticket (userid, tID, query, date, status) VALUES (:userid, :ticketid, :query, :timestamp, :status)");

$stmt->bindParam(':userid', $userid, PDO::PARAM_STR, 100);
$stmt->bindParam(':tID', $ticketid, PDO::PARAM_STR, 100);
$stmt->bindParam(':query', $query, PDO::PARAM_STR, 100);
$stmt->bindParam(':date', $date, PDO::PARAM_STR, 100);
$stmt->bindParam(':status', $status, PDO::PARAM_STR, 100);

if($stmt->execute()) {
echo "Ticket has successfully been added";
}
else {
echo "Didnt work";
}

$db = null;


Any advice?

Answer

You bind paramater tID and date

$stmt->bindParam(':tID', $ticketid, PDO::PARAM_STR, 100);
$stmt->bindParam(':date', $date, PDO::PARAM_STR, 100);

But you named it :ticketid and timestamp in your query.

VALUES (:userid, :ticketid,:query, :timestamp,

So you have two Options.

First rename Parameter in bind Statement:

$stmt->bindParam(':ticketid', $ticketid, PDO::PARAM_STR, 100);
$stmt->bindParam(':timestamp', $date, PDO::PARAM_STR, 100);

or change your Statement to:

VALUES (:userid, :tID,:query, :date,