PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:未定义参数

时间:2014-04-17 13:41:02

标签: php pdo

以下是我的代码,我无法解决此错误。任何帮助表示赞赏。我正在尝试更新数据库中的表。

    public function updateUnit($params){
    $user = 'monil';
    $password = 'Masters123';   
    $dbh = new \PDO('mysql:host=127.0.0.1;dbname=tcsdb', $user, $password);

    $task=array(':UnitCode'=>$params['UnitCode'],':UnitDescription'=>$params['UnitDescription']    ,
      ':UnitName'=>$params['UnitName'], ':UnitID'=>$params['UnitID']);
    echo $params['UnitID'];

    $sth = $dbh->prepare('UPDATE unit SET UnitCode = :UnitCode,'
        . 'UnitDescription = :UnitDescription,UnitName = :UnitName WHERE UnitId=:UnitId');
    $sth->execute($task); 

    return true;
}

3 个答案:

答案 0 :(得分:10)

:UnitID != :UnitId

参数区分大小写。

答案 1 :(得分:1)

如果您使用"可能会出现相同的错误。" bindParam中的点

离。

$query = "select * from t where t1 = :foo.bar";
$stmt = $pdo->prepare($query);
$stmt->execute([':foo.bar' => 'blah']);

答案 2 :(得分:0)

错过了同样的错误:创建语句时冒号。

例如:  由于VALUES中缺少密码,因此以下语句会引发无效参数错误:冒号。

    con.Open()
    Dim trans As SqlTransaction = con.BeginTransaction()
    Try
        Dim insertStr As String = "insert into ReceiptOfItems (receiptCode, receiptDate, etc.) values(@receiptCode, @receiptDate, @etc)"
        Dim insertCmd As New SqlCommand(insertStr, con, trans)
        For Each dc As DataColumn In dt.Columns
            Dim param As New SqlParameter()
            param.ParameterName = dc.ColumnName
            param.SourceColumn = dc.ColumnName
            insertCmd.Parameters.Add(param)
        Next
        dta.InsertCommand = insertCmd

        dta.Update(ds)
        trans.Commit()
        con.Close()
    Catch ex As Exception
        trans.Rollback()
    End Try