0

我正在尝试使用 ajax 来更改密码选项。在这里,我在控制器中尝试了以下代码

       if ($this->request->is(array('post', 'put'))){
            $id=$this->Auth->user('id'); 
            $changePass=$_POST['cpass'];
                $cpasss=AuthComponent::password($changePass);
                echo $cpasss;
                $up=$this->User->updateAll(
                array('User.password'=>"$cpasss"),
                array('User.id'=>"$id")
            );      
        }

这里的哈希工作正常,我见过萤火虫,但问题是这里的密码没有在数据库中更新。如果我删除

$cpasss=AuthComponent::password($changePass);   

然后密码更新正常,但没有哈希。有人可以帮我解决这个问题吗?

4

2 回答 2

0

将您的代码更新为:

   if ($this->request->is(array('post', 'put'))){
        $id=$this->Auth->user('id'); 
        $changePass= $this->request->data['cpass'];
            $cpasss=AuthComponent::password($changePass);

            $up=$this->User->updateAll(
            array('User.password'=> $cpasss),
            array('User.id'=> $id)
        );      
    }

变化

  1. $_POST[]不是在 cakephp 中获取表单数据的正确方法。
  2. 无需将变量包含在双引号 ( ") 中。
于 2015-05-28T13:43:53.623 回答
0

没有必要在updateAll()这里使用。相反,只需使用保存数据save()并记住将主键与保存数据一起传递:-

if ($this->request->is(array('post', 'put'))) {
    $id = $this->Auth->user('id');
    $changePass = $this->request->data['User']['cpass'];

    $data = array(
        'id' => $id,
        'password' => AuthComponent::password($changePass)
    );
    $this->User->save($data);
}

如果您确实使用updateAll()它,则需要谨慎使用,因为它不会转义传递的字段。从文档中:-

$fields 数组接受 SQL 表达式。文字值应该使用 DboSource::value() 手动引用。

于 2015-05-28T14:03:33.863 回答