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 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥40 宿舍管理系统设计(c#)
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。