Yii Framework Forum: Fatal Error: Call To A Member Function Createcommand() On A Non-Object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Fatal Error: Call To A Member Function Createcommand() On A Non-Object I want to create login history wehn a member login but not. Rate Topic: -----

#1 User is offline   Muhammad Shahzad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-January 13
  • Location:Islamabad,Pakistan

Posted 07 February 2013 - 02:52 AM

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' ";
$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);
}

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"
0

#2 User is offline   karim gioca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 21-September 10

Posted 07 February 2013 - 03:08 AM

View PostShahzad Thathal, on 07 February 2013 - 02:52 AM, said:

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' ";
$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);
}

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);
}

1

#3 User is offline   Muhammad Shahzad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 29-January 13
  • Location:Islamabad,Pakistan

Posted 07 February 2013 - 05:00 AM

Thanks a lot karim gioca.
I just use these curly braces {} at line
"if($model->validate() && $model->login()){ my statement }"
and now it's working prefectly.
thanks again.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users