Inserting new password by overriding old password

橙三吉。 提交于 2019-12-02 09:13:53

You can't get password like this $_POST['Password'], because you haven't set this post variable.

You had to use:

echo CHtml::textField('password');
echo CHtml::textField('repeatPassword');
echo CHtml::hiddenField('email', $email);

'password' and 'repeatPassword' are names of POST vars

And in your controller you have too many mistakes, try this (check for typos):

if(isset($_POST['email']))
{
 $record=User2::model()->find(array(
'select'=>'userId, securityQuestionId, primaryEmail',
'condition'=>'primaryEmail=:email',
'params'=>array(':email'=>$_POST['email']))
);

if($record===null) {
                  echo "Email invalid";
                   }

else {
    if(isset($_POST['password']) && isset($_POST['repeatPassword']) && ($_POST['password'] === $_POST['repeatPassword']))
    {
        $record->password = $_POST['password'];
        if ($record->save()) {
             $this->render('saved');
        }
    }          

    $this->render('Password' array('email'=>$_POST['email']));
}

 }

    }
    else{
        $this->render('emailForm'); //show the view with the password field
    }

In your code if(isset($_POST['Password'])) won't ever execute, because after sending password you haven't set email variable. So you just $this->render('emailForm');. Thus we set it by CHtml::hiddenField('email', $email);

Upd. I strongly recommend you to read this guide. It will save a lot of time for you.

First of all the way you are handling the form is certainly not Yii-ish which means it's not really the way to go.

The way you should handle this is by creating an object which extends from the CFormModel and put all your logic code in there instead of in the controller.

Now, if you want to continue working with your piece of code, then it would be best to place the following piece of code

$this->render('Password');

BELOW the if isset password stuff.

For your problem, the reason why your password isn't being updated is because the query you created is not being executed. If we take a look here then we can see that the following piece of code should be added:

$command->execute();

Which will execute your piece of sql.

Something like this...

$user = User::find('email = :email', ':email' => $_POST['email']);

if( empty($user) )
  return;

$user->password = $_POST['password'];
$user->save();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!