dongyan7988 2017-03-28 03:54
浏览 139
已采纳

我一直得到无效的参数号:数字或绑定变量不是yii2中的数学标记数

I am doing a simple update in yii2 controller inside of a for loop. When doing so I keep getting sql error: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

My code is below:

for($x = 0; $x < $existingProductCount; $x++)
        {
            $connection = Yii::$app->db;
            $params = [
                ':idcampaign_products' => intval(Yii::$app->request->post("idcampaign_product")[$x]),
                ':existing_products' => intval(Yii::$app->request->post("dynamicExistingProducts")[$x]),
                ':existing_billing_cycle' => Yii::$app->request->post("dynamicExistingBillingCycle")[$x],
                ':existing_trial_length' => Yii::$app->request->post("dynamicExistingTrialLength")[$x],
                ':existing_trial_price' => Yii::$app->request->post("dynamicExistingTrialPrice")[$x],
                ':existing_reg_price' => Yii::$app->request->post("dynamicExistingRegPrice")[$x]
            ];
            $connection->createCommand()
            ->update('campaign_products',
                [
                    'product_id'=>$params[':existing_products'],
                    'campaign_billing_cycle'=>$params[':existing_billing_cycle'],
                    'trial_length'=>$params[':existing_trial_length'],
                    'trial_price'=>$params[':existing_trial_price'],
                    'regular_price'=>$params[':existing_reg_price']
                ],
                'idcampaign_products=:idcampaign_products')
            ->bindValues($params)
            ->execute();
            //var_dump($params);

        }
  • 写回答

1条回答 默认 最新

  • donmqryh49993 2017-03-28 06:32
    关注

    You are not doing this properly. When you use query builder with an array you don't have to bound values - it's done automatically. With your code only :idcampaign_products needs to be bound and since you pass 6 values that is why the error pops.

    Do it like that:

    $connection->createCommand()
        ->update('campaign_products', [
            'product_id' => $params[':existing_products'],
            'campaign_billing_cycle' => $params[':existing_billing_cycle'],
            'trial_length' => $params[':existing_trial_length'],
            'trial_price' => $params[':existing_trial_price'],
            'regular_price' => $params[':existing_reg_price']
        ], ['idcampaign_products' => $params[':idcampaign_products']])
        ->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动