## get a list of the field names my $keys = join (', ', @keys); ## get number of placeholders required (number of field names) my $placeholder_count = @data; ## make $placeholder_count number of ?'s with a comma after each my $placeholders = '?, ' x $placeholder_count; ## remove last space and comma $placeholders = substr($placeholders, 0, - 2); ## make a string containing each piece of data separated by commas my $data = '"' . join('", "', @data) . '"'; my $dbh = DBI->connect("DBI:mysql:database=$database;host=localhost", $username, $password, {'RaiseError' => 1}); my $sth = $dbh->prepare("INSERT INTO $table ($keys) VALUES ($placeholders)") or die $dbh->errstr; $sth->execute($data) or die $dbh->errstr;