2014-09-19 41 views
0
到INSERT

我想在阵列,看起来像插入时:错误:“无效的参数个数:参数没有被定义的”使用数组中PDO

$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')); 

//跟踪($价格);

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

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

(由

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(); 
} 
+0

您必须为每个$ insert行执行插入语句。而不是使用'$ stmt-> bindParam()'我会用'$ stmt->执行($的insertRow);'。你也必须像上面提到的那样修正错别字。 – VMai 2014-09-19 12:50:18

回答

1

正如幻影说,他的回答,你有一个错字。还有就是你的数组中event_price_currency键和:event_price_currency_id占位符准备()语句。如果修复不起作用,请尝试以下代码并检查错字。如果您遇到任何问题,请告知我。

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

这工作,谢谢您的解决方案。 – 2014-09-19 18:28:42

+0

很高兴我能帮助 – 2014-09-19 18:42:19

0

你有一个错字给出的行号有event_price_currency键入您的阵列中并:event_price_currency_id占位符在prepare()语句中。

1

您的查询预计此参数:

:event_price_currency_id 

哪个你没有提供。你,然而,供参数:

'event_price_currency' => 1 

这些事情之一是不喜欢对方。

1
$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') 
    ); 

我改变了错字,现在我没有得到一个错误,但是当我看到MySQL的只有1行插入:

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

它必须是:

event_id , price_type, price, price_currency, price_info; 
1, 5, 5, 1, aaaa; 
1, 8, 7, 1, bbbb; 
1, 1, 8, 1, cccc; 
相关问题