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.
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));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
narmi2 -> look more carefully.
the SQL would insert two rows.
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(':col umn' => $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
$stmt->execute(array(':col
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
ASKER