Link to home
Start Free TrialLog in
Avatar of narmi2
narmi2

asked on

Multiple inserts with PDO

Dear Experts,

The following script allows me to insert a single row into the database:

This is because of the array:

array(':column' => $unsafeValue)

Which is a single key:value array.  How can I modify this array so it inserts more than 1 rows into the database in one statement.

Something like this?

array(':column' => $unsafeValue, ':column' => $unsafeValue, ':column' => $unsafeValue, )

???

Please help.
$preparedStatement = $db->prepare('INSERT INTO table (column) VALUES (:column)');

$preparedStatement->execute(array(':column' => $unsafeValue));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of virmaior
virmaior
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of narmi2
narmi2

ASKER

That would still do 1 row with 2 columns.  I would like to know if it is possible to use the same technique, but with a modified array and insert into multiple rows.
narmi2 -> look more carefully.

the SQL would insert two rows.
hello narmi2, I can not see a way to have multiple inserts with a single statement like -
$stmt->execute(array(':column' => $unsafeValue, ':column' => $unsafeValue));

but this works for me -

$cal = 'Mixed Up';
$addy = 'www.experts.com';
$mail = 'way\'s to, go';

$stmt = $dbo->prepare('INSERT INTO games (addy) VALUES (:addy)');
$stmt->bindParam(':addy', $val,PDO::PARAM_STR);
$aAry = array($addy, $mail, $cal);
foreach($aAry as $val) $stmt->execute();
// will insert three rows with different values in each row

however to expand this to have more than one column added for each row would need more stuff,
as far as I can tell you will need some sort of for loop that does a separate  $stmt->execute();  for each row inserted