Inserting new password by overriding old password

喜你入骨 提交于 2019-12-02 16:50:19

问题


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


回答1:


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.




回答2:


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.




回答3:


Something like this...

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

if( empty($user) )
  return;

$user->password = $_POST['password'];
$user->save();


来源:https://stackoverflow.com/questions/13620878/inserting-new-password-by-overriding-old-password

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