error: 'Invalid parameter number: parameter was not defined' when using an array to INSERT in PDO

后端 未结 4 1289
臣服心动
臣服心动 2021-01-26 10:31

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         


        
相关标签:
4条回答
  • 2021-01-26 10:38
    $prices = array(
            array('event_id' => 1, 'event_price_type' => 5, 'event_price' => 5, 'event_price_currency_id' => 1, 'event_price_info' => 'aaaa'),
            array('event_id' => 1, 'event_price_type' => 8, 'event_price' => 7, 'event_price_currency_id' => 1, 'event_price_info' => 'bbbb'), 
            array('event_id' => 1, 'event_price_type' => 1, 'event_price' => 8, 'event_price_currency_id' => 1, 'event_price_info' => 'cccc')
        );
    

    i changed the typo, now I don't get an error but when i look at mysql there is only 1 row inserted:

    event_id , price_type, price, price_currency, price_info;
    0, 0, 0, 0, cccc;
    

    where it has to be:

    event_id , price_type, price, price_currency, price_info;
    1, 5, 5, 1, aaaa;
    1, 8, 7, 1, bbbb;
    1, 1, 8, 1, cccc;
    
    0 讨论(0)
  • 2021-01-26 10:48

    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();
    }
    
    0 讨论(0)
  • 2021-01-26 10:52

    You have a typo. There is event_price_currency key in your array and :event_price_currency_id placeholder in prepare() statement.

    0 讨论(0)
  • 2021-01-26 11:03

    Your query is expecting this parameter:

    :event_price_currency_id
    

    Which you're not supplying. You are, however, supplying this parameter:

    'event_price_currency' => 1
    

    One of these things is not like the other.

    0 讨论(0)
提交回复
热议问题