Take a look at the admin/common/functions.php file on line 228. There is a SQL statements that will retrieve the most recent display_order. Basically, you query the images table for the most recent display_order then add 1 to it. If its a new gallery, you should just start the display_order at 1. You can then use this value in your custom insert statement.