php - 使用 cake PHP 更新个人详细信息时,数据库上的密码会自动更改

标签 php mysql cakephp

如果我更新任何已提交的个人详细信息,则我会单击“保存”。它保存新数据并将我的登录密码也更改为随机值

public function update_personal_details() {
     $this->layout = null ;
     $this->autoRender = false;

     $response = array('success' => true);
     if ($this->request->isPost()) {
        if($this->User->exists($this->Auth->user('id'))) {
          try {          
            $this->User->read(null, $this->Auth->user('id'));
            $this->User->set('first_name',$this->request->data["first_name"]);
            $this->User->set('last_name',$this->request->data["last_name"]);
            $this->User->set('mobile',$this->request->data["mobile"]);
            $this->User->set('city',$this->request->data["city"]);
            $this->User->save();
          } catch (exception $ex) {
             $response['success'] = false;
          }
        }
     }  

     return json_encode($response);
  }   

最佳答案

为了确保 cakePHP 仅更新所需的列,您可以在 save 命令上传递数据和字段列表:

Model::save(array $data = null, boolean $validate = true, array $fieldList = array())

看看 Cookbook Save your Data

对你来说这应该有效:

$data = array();
$data['first_name'] = $this->request->data["first_name"]);
$data['last_name'] = $this->request->data["last_name"]);
$data['mobile'] = $this->request->data["mobile"]);
$data['city'] = $this->request->data["city"]);

$this->User->save(array('User' => $data), true, array('first_name', 'last_name', 'mobile', 'city'));

希望有帮助

关于php - 使用 cake PHP 更新个人详细信息时,数据库上的密码会自动更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27903340/

相关文章:

PHP echo 只重复行一次

适用于 Python 程序员的 PHP : UTF-8 Issues

php - Symfony2 中具有相同模式的多个安全防火墙

Mysql 正则表达式 : match a list of words in a paragraph with regex

mysql - 为什么前 2 个查询不返回任何内容?

php - 我可以停止 CakePHP 获取查询的所有行吗?

在 HATBM 表上使用 WHERE 和 COUNT 的 CakePHP GROUP BY

php - highcharts JSON 谷歌地球

mysql - 如何通过用户的 id 获取每个用户的姓名?

cakephp - CKEditor/CKFinder 与 CakePHP 2.0