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条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示