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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据