错误:“无效的参数编号:参数没有被定义的”使用数组中PDO以插入时的 [英] error: 'Invalid parameter number: parameter was not defined' when using an array to INSERT in PDO

查看:428
本文介绍了错误:“无效的参数编号:参数没有被定义的”使用数组中PDO以插入时的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在阵列看起来像插入:

  $价格=阵列(阵列('EVENT_ID'=大于1,'event_price_type'=> 5,event_price'=> 5,event_price_currency'=> 1 'event_price_info'=>'AAAA'),阵列('EVENT_ID =大于1,'event_price_type'=→8'event_price'=大于7,'event_price_currency'=大于1,'event_price_info'= >'BBBB'),阵列('EVENT_ID =大于1,'event_price_type'=大于1,'event_price'=→8'event_price_currency'=大于1,'event_price_info'=>'CCCC ));

//跟踪($价格);

我想用PDO但是我总是得到消息插入阵列到mysql:


  

警告:PDOStatement对象::执行()[pdostatement.execute]:SQLSTATE [HY093]:无效的参数编号:绑定变量的数目不

匹配的令牌数量

(由给定的行号

  {尝试
    $这个 - > dbh->的BeginTransaction();
    $语句= $这个 - > dbh-> prepare(INSERT INTO event_prices(EVENT_ID,价格类型,价格,price_currency_id,price_info)VALUES(:EVENT_ID,:event_price_type,:event_price,:event_price_currency_id,:event_price_info) );    的foreach($价格为$的insertRow){
        //跟踪($的insertRow);
        //现在遍历每个内部数组来匹配绑定值
        的foreach($的insertRow为$栏=> $值){
            //回声COMLOM WAARDE:{$}列,值:{$值}< BR />中;
            $ stmt-> bindParam(:{$}栏,$值);
        }
    }
    //现在DO EXECUTE
    $ stmt->执行();
    $这个 - > dbh->提交();
}赶上(PDOException $ E){
    回声错误! '$ E-方式>的getMessage();
    死();
}


解决方案

正如幻影说,他的回答,你有一个错字。还有就是你的阵列中的 event_price_currency 键和:在prepare()语句event_price_currency_id 占位符。如果固定不行,请尝试以下code和检查错字。让我知道,如果你面对任何问题。

 尝试
{
   $ DBH->的BeginTransaction();
   $ STH = $ DBH-> prepare(INSERT INTO event_prices(EVENT_ID,event_price_type,event_price,event_price_currency_id,event_price_info)值(,,,,)?????);  的foreach($价格为$价格)
  {
    的foreach($价格作为$行)
    {
        $数据[] = $行;
    }    $ STH->执行($的数据);
    $数据= NULL;
  }  $ DBH->提交();
}赶上(PDOException $ E)
{
  回声错误! 。 $ E-GT&;的getMessage();
  死();
}

I'm trying to insert in array that looks like:

$prices = array(array('event_id' => 1, 'event_price_type' => 5, 'event_price' => 5, 'event_price_currency' => 1, 'event_price_info' => 'aaaa'), array('event_id' => 1, 'event_price_type' => 8, 'event_price' => 7, 'event_price_currency' => 1, 'event_price_info' => 'bbbb'), array('event_id' => 1, 'event_price_type' => 1, 'event_price' => 8, 'event_price_currency' => 1, 'event_price_info' => 'cccc'));

// trace($prices);

I want to insert the array into mysql using PDO however i always get the message:

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in

(The line number given by the

try{
    $this->dbh->beginTransaction();
    $stmt = $this->dbh->prepare("INSERT INTO event_prices(event_id, price_type, price, price_currency_id, price_info) VALUES(:event_id, :event_price_type, :event_price, :event_price_currency_id, :event_price_info)");

    foreach($prices as $insertRow) {
        // trace($insertRow);
        // now loop through each inner array to match binded values
        foreach($insertRow as $column => $value){
            // echo "COMLOM WAARDE: {$column}, VALUE: {$value}<br/>";
            $stmt->bindParam(":{$column}", $value);
        }
    }
    // NOW DO EXECUTE
    $stmt->execute();
    $this->dbh->commit();
} catch (PDOException $e) {
    echo 'Error ! '.$e->getMessage();
    die();
}

解决方案

As Phantom said in his answer, You have a typo. There isevent_price_currency key in your array and :event_price_currency_id placeholder in prepare() statement. If fixing that doesn't work, try the following code and check the typo. Let me know if you face any problem.

try
{
   $DBH->beginTransaction();
   $STH = $DBH->prepare("INSERT INTO event_prices(event_id, event_price_type, event_price,  event_price_currency_id, event_price_info ) values (?, ?, ?, ?, ?)");

  foreach($prices as $price)
  {
    foreach($price as $row)
    {
        $data[] = $row;
    }

    $STH->execute($data);
    $data = NULL;
  }

  $DBH->commit();
}

catch(PDOException $e)
{
  echo 'Error ! ' . $e->getMessage();
  die();
}

这篇关于错误:“无效的参数编号:参数没有被定义的”使用数组中PDO以插入时的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆