This is just a guess, but it sounds like a problem with the wp cache. Try cleaning the cache for the post after you add the new rows and before you try to count them again. https://codex.wordpress.org/Function_Reference/clean_post_cache
<?php clean_post_cache( $id ) ?>