So from this, it looks like the order orders.OrderReferenceID is being compared to -1, which is the default value of $colname_rsorders. The OrderReferenceID session value is not being set on this page, so you are always comparing to -1. Make sure the OrderReferenceID is being set to a session variable when the order is inserted into the database.