Reputation: 203
I have the following update code in my controller. The sql query is executing correctly and I can see the updated result in backend. But while executing its showing error, 'CDbCommand failed to execute the SQL statement'. Is there any other way to write this statement. Contoller codes are as follow.
public function actionactiveuser()
{
$user_id=$_GET['id'];
$query = "UPDATE user SET status = '1' WHERE id = '$user_id'";
Yii::app()->db->createCommand($query)->queryAll();
$this->render('login');
}
It is showing error:
CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error. The SQL statement executed was: UPDATE user SET status = '1' WHERE id = '80'
Upvotes: 0
Views: 5780
Reputation: 5094
change this
Yii::app()->db->createCommand($query)->queryAll();
to
Yii::app()->db->createCommand($query)->execute();
queryAll() is used for select queries. execute() is for Update and Insert queries
BTW For the safety you should do something like this
$query = "UPDATE user SET status = :status' WHERE id = :id";
Yii::app()->db->createCommand($query)->execute(array(':status' => '1',':id'=>$user_id ));
Upvotes: 4