dongtuo0828 2013-04-19 08:16
浏览 108
已采纳

Yii SQLSTATE [HY093]:参数号无效:未定义参数

I have looked at the other questions concerning this error message, but all appear to have something to do with a miss match between a parameter and its binding, or missing bindings, or something like that. Here is my code. Can anyone here see the error? This is in a CDbMigration class.

$this->insert('company', array(
    'name' => 'Site Administrator',
    ));
$companyId = $this->dbConnection->getLastInsertID();

$sql  = 'insert into `user` (`company-id`, `login-id`, `password`) ';
$sql .= 'values (:company-id, :login-id, :password)';
$command = $this->dbConnection->createCommand($sql);
$command->bindValue(':company-id', $companyId, PDO::PARAM_INT);
$command->bindValue(':login-id', 'admin');
$command->bindValue(':password', 'admin');
$command->execute();

The first insert performs properly, the second gives the following error:

Exception: CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined. The SQL statement executed was: insert into `user` (`company-id`, `login-id`, `password`) values (:company-id, :login-id, :password). Bound with :company-id='6', :login-id='admin', :password='admin' (C:\eclipse-php-3.0.2\workspace\interview\lib\yii\db\CDbCommand.php:357)

For the record, I realize that I need to do something to the password so that it is not stored in plain text. I will deal with that later. In addition, the user table is defined in the migration class as follows:

$this->createTable('user', array(
    'id' => 'pk',
    'company-id' => 'integer not null',
    'login-id' => 'string unique not null',
    'password' => 'string not null',
    'full-name' => 'string',
    'mobile-phone' => 'string',
    'other-phone' => 'string',
    'email' => 'string unique',
    ));
$this->addForeignKey('fk_user_company', 'user', 'company-id', 'company', 'id'
    , 'restrict');

展开全部

  • 写回答

2条回答 默认 最新

  • dongsaohu6429 2013-04-19 08:26
    关注

    Why not continue in the same way and have:

    $this->insert('user', array(
       'company-id' => (int)$companyId,
       'login-id' => 'admin',
       'password' => 'admin'
    ));
    

    ?

    Also, using dashes instead of underscores is not the best idea you have had for a table column name. Later you'll want to use CActiveRecord to reference the login id for example, but

    $model->login-id;
    

    is invalid so you'd have to

    $model->{'login-id'} // or
    $model->getAttribute('login-id');
    

    which is a pain. Overall i advise you to switch it to underscore.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部