Be careful. If you query the database to find the highest id, there is a possibility that another process will do an insert after your query and you both will try to use the same number.
So instead, do your sql INSERT first, then get the ID of that transaction and use it to name your data file.
You didn't say what database you were using, but if it is mysql, mysql_insert_id() will return the value.