I want to save loginhistory when a member login in.
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
//var_dump($model->attributes);
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$connection = Yii::app()->db;
$username = $model->attributes['username'];
$password = $model->attributes['password'];
$sql_id = "SELECT member_id FROM members WHERE username=’$username’ AND password = ‘$password’ ";
when a member give real username and pass then it save loghistory successfully into database but when a user enter wrong password then it not validate and not call login function and show this error!
"Fatal error: Call to a member function createCommand() on a non-object"
if i’m not wrong it miss something in your code neat "if($model->validate() && $model->login()) "
i think you didn’t use the { } ?
and it’s normal that it gives you the error because you must put all the saving procedure into the "if($model->validate() && $model->login()) "
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
//var_dump($model->attributes);
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login()) {
$connection = Yii::app()->db;
$username = $model->attributes['username'];
$password = $model->attributes['password'];
$sql_id = "SELECT member_id FROM members WHERE username='$username' AND password = '$password' ";
$id = $connection->createCommand($sql_id)->queryAll();
$id_save = $id[0]['member_id'];
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO loginhistory SET member_id='$id_save',login_ip ='$ip',login_time =now()";
$connection->createCommand($sql)->execute();
}
$this->redirect(Yii::app()->user->returnUrl);
}