Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:18:47+00:00 2026-06-13T01:18:47+00:00

I am trying to execute multiple sql statements in yii , this is to

  • 0

I am trying to execute multiple sql statements in yii , this is to reduce round trips to the server . This is for a field called niche specialization . A maximum of three enteries are allowed for a user , however the field is optional . This is the code :

$sql = " Delete from cur_niche 
         where userid =:id ; " ;
$var2 = array_filter($object->nspecial) ;
  if(!empty($var2))                 
                    {    $str = ""  ; 
                         $i = 0 ; 
                         foreach($var2 as $n) 
                         {   if($i==0)
                         $str.= "(:id,:n".$i.")" ;
                         else 
                         $str.= ", (:id,:n".$i.")" ; 
                         $i++ ;  
                         }
        $sql .= "Insert into cur_niche (userid,nspecial) Values ".$str." ; " ;
                    }
$command = Yii::app()->db->createCommand($sql)  ;
 $command->bindParam(":id",$id,PDO::PARAM_STR) ;
  if(!empty($var2))                     
    {     $i = 0 ;
         foreach($var2 as $n) 
            { 
             $str = ":n".$i ; 

            $command->bindParam($str,$n,PDO::PARAM_STR) ;
              $i++ ; 
                }
        }   
$result = $command->execute() && $result ; 

I have tried dumping all my variables , the sql statement is formed correctly , the $object has the correct data from the form , In the binding the $str is working fine .
However I am facing the following issue :
1) If there is only one entry in the niche spcialization array($object->nspecial) , the entry is working fine .
2) However if more than one field is filled , it fails to execute the insert , this is because the userid and nspecial fields form a composite primary key .

Doesnt the composite primary key depend on both values forming a unique combination (assuming that the user enters unique values in various fields) ?
If I remove the primary key constraint it just duplicates the value of one field with two rows in the db .What am I doing wrong here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:18:48+00:00Added an answer on June 13, 2026 at 1:18 am

    Found the bug . For the ‘foreach’ loops I should be binding by value not by parameter , since it references to the most recent value of the parameter , thus duplicating values .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using VS2005 and SQL Server 2005. I am trying to execute multiple
I am using VS2005 and SQL Server 2005. I am trying to execute multiple
I'm trying to grab multiple rows from SQL Server and then manipulate them in
I am using Nhibernate with SQL server 2008. I am trying to execute the
In the following command i am trying to ssh commands and execute the multiple
While trying to execute the following lines only the last two statements are displayed(Here
I'm trying to execute command prompt commands and read the output in C#. This
When I'm trying to execute a functional unittest of a module within my Yii
I'm trying to execute a sql script using the task in SSIS. My script
In my Script Component, am trying to execute Stored Procedure => which return multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.