Yii Framework Forum: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: - Yii Framework Forum

Jump to content

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

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Rate Topic: -----

#1 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 06 May 2011 - 06:36 AM

Hi,


Although the inserts go in the table correctly, I get the following exception while executing.

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. The SQL statement executed was: insert into bus_products (bus_type_id,quantity,product_id,cat_id,subcat_id) values (:bus_type_id,:quantity,:product_id,:cat_id,:subcat_id)


the following is my controller code.

public function actionCreate()
	{
		$connection=Yii::app()->db;
		$model=new BusType;
         $model2=new BusProducts;
		// Uncomment the following line if AJAX validation is needed
 $this->performAjaxValidation($model);
  //Get the number of models you will be working with 
$maxRows =count($_POST['quantity']);


		if(isset($_POST['BusType']))
		{
			$model->attributes=$_POST['BusType'];
				$model2->attributes=$_POST['BusProducts'];
			
			if($model->save())
				{
					$bus_type_id=$model->bus_type_id;
					 for ($i = 0; $i <= $maxRows ; $i++) 
            {
				 $quantity = $_POST['quantity'][$i];
			 $product_id = $_POST['product_id'][$i];
			  $cat_id = $_POST['cat_id'][$i];
			   $subcat_id = $_POST['subcat_id'][$i]; 
			$sql = "insert into bus_products (bus_type_id,quantity,product_id,cat_id,subcat_id) values (:bus_type_id,:quantity,:product_id,:cat_id,:subcat_id)"; 
 
					  
			
$command=$connection->createCommand($sql);
$command->bindParam(":quantity",$quantity,PDO::PARAM_STR);
$command->bindParam(":product_id",$product_id,PDO::PARAM_STR);
$command->bindParam(":cat_id",$cat_id,PDO::PARAM_STR);
$command->bindParam(":subcat_id",$subcat_id,PDO::PARAM_STR);
$command->bindParam(":bus_type_id",$bus_type_id,PDO::PARAM_STR);
$command->execute();
		
					$model2->save();  
							}
				$this->redirect(array('view','id'=>$model->bus_type_id));
		}
		}

		$this->render('create',array(
			'model'=>$model,
			'model2'=>$model2,
		));
	}
	



Can anybody please tell me why i'm getting this error and how to correct it.
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 06 May 2011 - 07:10 AM

Just a question, why are writing all this code, when $model->save does all and correctly?
0

#3 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 06 May 2011 - 07:18 AM

View Postzaccaria, on 06 May 2011 - 07:10 AM, said:

Just a question, why are writing all this code, when $model->save does all and correctly?



With model->save only the last entry was getting inserted in the table. Thats y i had to write the code.
0

#4 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 06 May 2011 - 07:23 AM

Fixed it. The for condition was worng. I should be
 for ($i = 0; $i < $maxRows ; $i++) 
and not
 for ($i = 0; $i <= $maxRows ; $i++) 
.

Sorry for the trouble. My bad.
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