dongtuo0828 2013-04-19 16: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 16: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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?