PHP PDO错误 - :参数号无效

时间:2016-07-21 11:28:27

标签: php sql pdo statements

我是第一次尝试使用PDO插入数据库但我一直收到错误

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

谷歌告诉我,我插入的值与某些地方存在错误匹配,但我可以告诉他们加起来

    $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;

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

绑定参数/** * Deletes the local Twitter user session from this app. This will not remove the system Twitter account nor make a network request to invalidate the session. * * @param userID ID of the user to log out */ Twitter.sharedInstance().sessionStore.logOutUserID(userId) 和日期

tID

但您在查询中将其命名为$stmt->bindParam(':tID', $ticketid, PDO::PARAM_STR, 100); $stmt->bindParam(':date', $date, PDO::PARAM_STR, 100); :ticketid

timestamp

所以你有两个选项。

首先在绑定语句中重命名参数:

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

或将您的声明更改为:

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