【发布时间】:2026-01-04 04:30:01
【问题描述】:
在第 33 行和第 54 行出现这些错误(我的语句执行两个查询的行),但不知道为什么。看起来我的绑定变量都排成一行。有什么想法吗?
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens... on line 33
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens... on line 54
另外,我担心这是否会起作用并且绑定将接受具有正确值的 $SID2 变量,但在解决上述错误之前无法判断: $SID = $dbh->lastInsertId(); $SID2 = $SID;
以下是相关代码:
$dbh = new pdo('mysql:host='.$hostName.';dbname='.$dataBaseName, $user, $pass);
if(isset($_POST ['submit'])){
$user_ID = get_current_user_id();
$sql = "INSERT INTO stories(ID,
category,
genre,
rating,
story_name,
active) VALUES (
:ID,
:genre,
:rating,
:story_name,
:active)";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':ID', $user_ID, PDO::PARAM_STR);
$stmt->bindParam(':category', $_POST['category'], PDO::PARAM_STR);
$stmt->bindParam(':genre', $_POST['genre'], PDO::PARAM_STR);
$stmt->bindParam(':rating', $_POST['rating'], PDO::PARAM_STR);
$stmt->bindParam(':story_name', $_POST['story_name'], PDO::PARAM_STR);
$stmt->bindParam(':active', $a = 0, PDO::PARAM_STR);
$stmt->execute(); //line 33---error line
$SID = $dbh->lastInsertId();
$SID2 = $SID;
$sql2 = "INSERT INTO writing(ID,
SID,
text,
position,
approved) VALUES (
:ID,
:SID,
:text,
:position,
:approved)";
$stmt2 = $dbh->prepare($sql2);
$stmt->bindParam(':ID', $user_ID, PDO::PARAM_STR);
$stmt->bindParam(':SID', $SID2, PDO::PARAM_STR);
$stmt->bindParam(':text', $_POST['text'], PDO::PARAM_STR);
$stmt->bindParam(':position', $b = 0, PDO::PARAM_STR);
$stmt->bindParam(':approved', $c = 0, PDO::PARAM_STR);
$stmt->execute(); //line 54--error line
【问题讨论】:
-
嗨,Jaw,你还需要这方面的帮助吗