0

I have the following code in my controllers, the insert query is not happening. I'm using postgresql for database.

 $connection = Yii::app()->db2 ;
 $command1 = "INSERT INTO USERS (username, user_type) VALUES (:username,:user_type)";
 $cmd1 = $connection->createCommand($command1);
 $cmd1->bindParam(":username",$username,PDO::PARAM_STR);
 $cmd1->bindParam(":user_type",'USER',PDO::PARAM_STR);
 $cmd1->execute();
 echo "Completed...";

What's wrong in the code. the echo "Completed..."; is not printing. I have enabled my postgressql logs the insert query is not executed. No errors display in browser, only blank page is displaying.

the below formate is working to insert into my table. and the query is logged into postgresql log.

$command->insert('users', array(
                                              'username'=>$username,
                                              'user_type'=>'USER',
                                              ));

Thanks in Advance.

S-Man
  • 22,521
  • 7
  • 40
  • 63
ungalnanban
  • 9,539
  • 10
  • 44
  • 55

2 Answers2

4

Try use 'bindValue()' instead of 'bindParam()':

$connection = Yii::app()->db2 ;
 $command1 = "INSERT INTO USERS (username, user_type) VALUES (:username,:user_type)";
 $cmd1 = $connection->createCommand($command1);
 $cmd1->bindValue(":username",$username,PDO::PARAM_STR);
 $cmd1->bindValue(":user_type",'USER',PDO::PARAM_STR);
 $cmd1->execute();
 echo "Completed...";
Daniel Vaquero
  • 1,315
  • 1
  • 8
  • 13
  • Here: http://stackoverflow.com/questions/14413326/confusion-between-bindvalue-and-bindparam explain in detail the difference between 'bindValue()' and 'bindParam()'. I hope that help you :D – Daniel Vaquero Sep 18 '13 at 07:59
1

You do not have to bind the value 'USER', you can write it directly in query like this:

 $connection = Yii::app()->db2 ;
 $command1 = "INSERT INTO USERS (username, user_type) VALUES (:username,'USER')";
 $cmd1 = $connection->createCommand($command1);
 $cmd1->bindParam(":username",$username,PDO::PARAM_STR);
 $cmd1->execute();
 echo "Completed...";
Arfeen
  • 2,553
  • 5
  • 29
  • 48