Multiple insert with prepared statement

时间:2015-06-25 09:59:17

标签: php mysql pdo

I want to insert 5 llines in my db, however the following

$qry = $db->prepare('INSERT IGNORE INTO table (foo, bar) VALUES 
    (?,?),
    (?,?),
    (?,?),
    (?,?),
    (?,?)');
$qry->execute(array(
    array($foo1, $bar),
    array($foo2, $bar),
    array($foo3, $bar),
    array($foo4, $bar),
    array($foo5, $bar)
));

gives me this error

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

What should I do to make it work ?

1 个答案:

答案 0 :(得分:1)

Remove the extra arrays in the execute()

$qry = $db->prepare('INSERT IGNORE INTO table (foo, bar) VALUES 
    (?,?),
    (?,?),
    (?,?),
    (?,?),
    (?,?)');
$qry->execute(array(
    $foo1, $bar,
    $foo2, $bar,
    $foo3, $bar,
    $foo4, $bar,
    $foo5, $bar
));