koleto
(Koleto Pz)
July 22, 2009, 8:40pm
1
I am trying to build a form which must send new password to users who have forgotten them. I have build a Model and View but I have big problem with Controller. View consist of two input fields - one for email of the user and one for capcha. Here is the actionForgottenPass from the SiteController class:
public function actionForgottenPass()
{
//ForgottenPassword is CActiveRecord child class
$forgotten=new ForgottenPassword;
//if the form is submitted
if(isset($_POST['ForgottenPassword']))
{
$forgotten->attributes=$_POST['ForgottenPassword'];
$forgotten->email=$_POST['ForgottenPassword']['email'];
$forgotten->verifyCode=$_POST['ForgottenPassword']['verifyCode'];
//if the form have no errors
if($forgotten->validate())
{
//loop for generating random string
for($i=0; $i<8; $i++){
$randnum = mt_rand(0,61);
if($randnum < 10){
$randstr .= chr($randnum+48);
}else if($randnum < 36){
$randstr .= chr($randnum+55);
}else{
$randstr .= chr($randnum+61);
}
}
$c=new CDbCriteria;
$mail = $forgotten->email;
$c->condition='\'emai\'='.$forgotten->email;
$c->limit=1;
//we do md5 so we can make string with numbers and letters
$a=array('password'=>md5($randstr));
if(ForgottenPassword::model()->updateAll($a, $c))
{
//sends email
$headers="From: {".Yii::app()->params['adminEmail']."}\r\n";
$body = "The system generated new password for your Netcollector account, as you requested. Use your username and new password to log in. \n\n";
$body .= "Password:$forgotten->password\n\n";
$body .= "We strongly recomand you to change your new password, with something that you will remember easily. You can do this from you user account settings.\n\n";
$body .= "- NetCollector team";
mail( $forgotten->email,'Forgotten Password',$body,$headers);
//if everything is ok display the message
Yii::app()->user->setFlash('forgotten','Your new password have been send to your email. If again you can\'t log in, dont\'t be afraid to ask us for help. Just write one email in our contact form and will take care for the problem.');
$this->refresh();
}
}
}
I believe that the problem is in the ForgottenPassword::model()->updateAll or something connected with it. The error that i receive is:
CDbException
Description
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@abv.bg LIMIT 1' at line 1
From the application.log I see that the query looks like this:
UPDATE `User` SET `password`=:yp0 WHERE 'emai'=test@test.test LIMIT 1
I will be very happy if someone help me.
Spyros
(Spyros)
July 23, 2009, 4:35am
2
test@test.test have to be in quotes like this
emai
=‘test@test.test’ LIMIT 1
andy_s
(Arekandrei)
July 23, 2009, 5:30am
3
Btw, emai or email ?
And a better way is to bind params like this:
$c->condition='email=:email';
$c->params=array(':email'=>$forgotten->email);
koleto
(Koleto Pz)
July 23, 2009, 8:48pm
4
I change the code as andy_s said and it works perfect now. Thank you a lot