How to insert an array into a single MySQL Prepared statement w/ PHP and PDO

You could build the query programatically…:

$sql="INSERT INTO table (memberID, programID) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($data as $row) {
    $insertQuery[] = '(?, ?)';
    $insertData[] = $memberid;
    $insertData[] = $row;
}

if (!empty($insertQuery)) {
    $sql .= implode(', ', $insertQuery);
    $stmt = $db->prepare($sql);
    $stmt->execute($insertData);
}

Leave a Comment