The best move would probably be to add an identity field, and just grab the next/prev image by ID. You could also use an
ORDER BY, to order by filename, date, or something else, which would make sure they come out in the same order, so you could make sure the next/prev image is correct.