There is a one-to-many relationship between vials(block_id) and blocks(id), but your code doesn't seem to use it in any way. If there are many rows in vials for each row in blocks, you have the opportunity to optimize away many of the INSERT followed by SELECT on the blocks table. Storing the names (pluralisation is really confusing here) and id pairs in a hash will definately speed things up.
Also, most databases with autonumbering offers a special function for retrieving the number which should perform better than doing a SELECT to find the latest inserted id. This will especially help if the names column isn't indexed.