Last insert id producing 0 in few enteries

You were already told what to do -

What php version are you using?

If for some reason you are not using php8+, add the following line of code before the point where you make the database connection to enable exceptions for the mysqli extension -
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

You should also test the last insert id value in your code and display an error message, and halt execution if it is a zero (there’s a chance it is not, but it is being converted to a zero when used in the later queries.) There’s also a chance you have happened upon a bug in either php or the database server you are using.

It would help if you posted your database table definitions, because this could be occurring due to an out of range value combined with the database server not being set to strict mode and it is truncating the value instead of reporting an error for the value.