Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Tags:

php

pdo

Below is my code, I am not able to resolve this error. Any help is appreciated. I am trying to update a table in my database.

    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;
}
like image 454
Monil Shah Avatar asked Apr 17 '14 13:04

Monil Shah


2 Answers

:UnitID != :UnitId

Parameters are case-sensitive.

like image 107
Niet the Dark Absol Avatar answered Nov 09 '22 11:11

Niet the Dark Absol


The same error arise when you missed : colon while creating statement.

ex: Below statement throws invalid parameter error as password in VALUES is missing : colon.

$stmt = $db->prepare('INSERT INTO members (username,password) VALUES (:username, password)');
like image 1
Vaibs Avatar answered Nov 09 '22 12:11

Vaibs