duandianwen1723 2015-05-02 23:19
浏览 34
已采纳

Yii密码加密

I can't understnad why this is not working. This code snippt is in my userscontroller.php Does it need to be elsewhere?

public function actionCreate()
{
    $model=new Users;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Users']))
    {
        $model->attributes=$_POST['Users'];
        $model->password = crypt_SHA_512($model->password,'salt');
        if($model->save())
            $this->redirect(array('view','id'=>$model->users_id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}
  • 写回答

2条回答 默认 最新

  • dongshuming7131 2015-05-03 19:27
    关注

    You are making one mistake of not handling errors, and this is why you cannot understand why this is not working.

    Use this snippet to understand why.

     if($model->save())
         $this->redirect(array('view','id'=>$model->users_id));
     else
         print_r($model->getErrors());
    

    The correct way would be to call the model validate() function before saving, and display the error in your view.

     if ($model->validate()) {
        if ($model->save()) {
            $this->redirect(array('view','id'=>$model->users_id));
        }
     }
    
     $this->render('create',array(
            'model'=>$model
     ));
    

    and in your view :

    <?php 
        $form=$this->beginWidget('CActiveForm' ...);
     ?>
    
      <?php echo $form->errorSummary($model); ?>
    
      <div class="row">
          <?php echo $form->labelEx($model,'username'); ?>
          <?php echo $form->textField($model,'username'); ?>
          <?php echo $form->error($model,'username'); ?>
      </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大