François M.
François M.

Reputation: 4278

Multiple insert with prepared statement

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 ?

Upvotes: 0

Views: 54

Answers (1)

Daan
Daan

Reputation: 12236

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

Upvotes: 1

Related Questions