Update query

Hi alll,

Can anybody tell me how to write an updte query using DAO. Its not explained in the documentation.

My code is as follows:


 $connection=Yii::app()->db;

		 $jc_id=$_GET['id'];

		 $state=$_GET['state'];

		$sql='update job_cards set status =:status where jc_id=:jc_id';

		$command->bindParam(":status",$state,PDO::PARAM_STR);

$command->bindParam(":jc_id",$jc_id,PDO::PARAM_STR);

		$command=$connection->createCommand($sql)->execute();

This code gives as error

Fatal error: Call to a member function bindParam() on a non-object in G:\xampp\htdocs\testyii\damodar\protected\controllers\JobCardsController.php on line 136

Please advice what to do.

Thanks

I fixed it myself. I changed the code to


 $connection=Yii::app()->db;

		 $jc_id=$_GET['id'];

		 $state=$_GET['state'];

		$sql='update job_cards set status =:status where jc_id=:jc_id';

		$command=$connection->createCommand($sql);

		$command->bindParam(":status",$state,PDO::PARAM_STR);

		$command->bindParam(":jc_id",$jc_id,PDO::PARAM_STR);

  //$command->bindValue(':status', $state, PDO::PARAM_STR);

		$command->execute();

You should check out the saveAttributes method in CActiveRecord

saveAttributes()

You should then get something like




$jc_id=$_GET['id'];

$jobCardModel = JobCards::model()->findByPk($jc_id)

$state=$_GET['state'];

$jobCardModel->saveAttributes(array('status' => $state));



edit: read your post wrong and edited mine :P

hello, i’m trying to use this update method to store a float value but instead of writing the correct value it always convert it to “1,000…” … if i change the datatype in db from float to varchar storing is correct

:blink: ???

Be careful of SQL Injection attacks





$state=$_GET['state'];

$jobCardModel->saveAttributes(array('status' => $state));




$state varible shode be validate according to:

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#saveAttributes-detail