Inserting new password by overriding old password

2019-03-03 19:17发布

In yii i am creating project. After validation of user's entered email, i am displaying password.php file which is having textfield for entering new password. Password.php=

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'email-form',
    'enableClientValidation'=>true,
   ));
        echo CHtml::textField('Enter new password');
        echo CHtml::textField('Repeat password');
        echo CHtml::submitButton('Submit');
        $this->endWidget();

When user will enter new password and click on submit button i want to insert this new password into User table's password field, in such a way that it overright old password.

In controller i had created method as-

public function actionCreate(){

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 {
    echo "email exists";


      $this->render('Password');
      if(isset($_POST['Password']))
        {

        $command = Yii::app()->db->createCommand();
        $command->insert('User', array(
                    'password'=>$_POST['password'] ,
                    //'params'=>array(':password'=>$_POST['password'])

        }          



 }

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

But its not inserting new password. So How can i implement this...Please help me

标签: yii passwords
3条回答
我想做一个坏孩纸
2楼-- · 2019-03-03 19:57

Something like this...

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

if( empty($user) )
  return;

$user->password = $_POST['password'];
$user->save();
查看更多
forever°为你锁心
3楼-- · 2019-03-03 20:09

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.

查看更多
你好瞎i
4楼-- · 2019-03-03 20:10

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.

查看更多
登录 后发表回答