increments $i outside the insert into db foreach