PHP PDO错误-:无效的参数号 [英] PHP PDO Error - : Invalid parameter number

查看:95
本文介绍了PHP PDO错误-:无效的参数号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PDO首次插入数据库,但我不断收到错误消息

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告诉我某处与我要插入的值不匹配,但据我所知它们相加

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;

有什么建议吗?

推荐答案

您绑定参数tID和日期

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

但是您在查询中将其命名为:ticketidtimestamp.

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

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

所以您有两个选择.

绑定语句中的第一个重命名参数:

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,

这篇关于PHP PDO错误-:无效的参数号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆