What's wrong with this code? i want to insert multiple rows with same movie id and different actor id. when i run this with $actors = array(1,2,3,4); (or any value) and $movid = 1, all the four rows are inserted as values (1, 4).
$create_query = "INSERT INTO `tableName`(`movId`, `actId`) VALUES";
$comma = "";
foreach ($actors as $key=>$value) {
$create_query .= $comma . "(:movie" . $key . ", :actor" . $key . ")";
$comma = ", ";
}
$query = $db->prepare($create_query);
foreach ($actors as $key=>$value) {
echo $movid . ', ' . $value //correct output (1, 1), (1, 2) ...
$query->bindParam(":movie" . $key, $movid, PDO::PARAM_INT);
$query->bindParam(":actor" . $key, $value, PDO::PARAM_INT);
}
print_r($query);
//output is
/*INSERT INTO `tableName`(`movId`, `actId`) VALUES(:movie0, :actor0), (:movie1, :actor1), (:movie2, :actor2), (:movie3, :actor3)*/
if ($query->execute()) {
$return = $query->rowCount();
$query->closeCursor();
}
when i use separate queries for inserting, it works correctly.