What are the best practices for dynamically generating and inserting multiple entries into a database using PHP?

When dynamically generating and inserting multiple entries into a database using PHP, it is best to use prepared statements to prevent SQL injection attacks and improve performance. Additionally, looping through the data to be inserted and executing the prepared statement for each entry is an efficient way to handle multiple inserts.

// Assume $data is an array of arrays containing the data to be inserted
// Assume $pdo is a PDO object connected to the database

$sql = "INSERT INTO table_name (column1, column2) VALUES (:value1, :value2)";
$stmt = $pdo->prepare($sql);

foreach ($data as $entry) {
    $stmt->execute([
        'value1' => $entry['value1'],
        'value2' => $entry['value2']
    ]);
}