PHP PDO Error - : Invalid parameter number

烂漫一生 提交于 2019-12-25 17:40:06

问题


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?


回答1:


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,


来源:https://stackoverflow.com/questions/38503061/php-pdo-error-invalid-parameter-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!