Invalid parameter number: parameter was not defined Inserting data
Just to provide an answer - because this error is pretty common - here are a few causes:
The
:parameter
name does not match the bind by mistake (typo?). This is what happened here. He has:alias
in the SQL statement, but bound:username
. So when the param binding was attempted, Yii/PDO could not find:username
in the sql statement, meaning it was "one parameter short" and threw an error.Completely forgetting to add the
bindValue()
for a parameter. This is easier to do in Yii other constructs like$critera
, where you have an array or params ($criteria->params = array(':bind1'=>'test', ':bind2'=>'test)
).another possible reason is invalid character in the placeholder name
Weird conflicts with CDataProvider Pagination and/or Sorting when using
together
andjoins
. There is no specific, easy way to characterize this, but when using complex queries in CDataProviders I have had weird issues with parameters getting dropped and this error occurring.
One very helpful way to troubleshoot these issues in Yii is to enable parameter logging in your config file. Add this to your db
array in your config file:
'enableParamLogging'=>true,
And make sure the CWebLogRoute
route is set up in your log
section. This will print out the query that gave and error, and all of the parameters it was attempting to bind. Super helpful!
php error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
You don't need to surround your parameter name with '
. Remove '
before and after :password
. Change
$query = "INSERT INTO " . $this->table_name . "
(first_name,last_name,phone,password_hash)
values (:first_name,:last_name,:phone,':password')
";
to
$query = "INSERT INTO " . $this->table_name . "
(first_name,last_name,phone,password_hash)
values (:first_name,:last_name,:phone,:password)
";
Related Topics
PHP: Running Scheduled Jobs (Cron Jobs)
How to Send a Get Request from PHP
Sorting an Associative Array in PHP
Sum Values of Multidimensional Array by Key Without Loop
How to Get the Error Message For the Mail() Function
Laravel: Error [Pdoexception]: Could Not Find Driver in Postgresql
Pdo::_Construct(): Server Sent Charset (255) Unknown to the Client. Please, Report to the Developers
Special Characters in Property Name of Object
Download File Through an Ajax Call PHP
PHP - Get Bool to Echo False When False
How to Increase the Execution Timeout in PHP
Convert Dot Syntax Like "This.That.Other" to Multi-Dimensional Array in PHP