You use an If statement. It's really that simple. The state of the data should tell you whether the last action was an entrance or an exit. When the student scans at the entrance you query the database and IF there last action was an entrance then you display the message. Likewise, when they scan at the exit you display the message IF there last action was an exit.

How you represent the last action in the data is up to you. Maybe you have a Boolean column where True is entrance and False is exit. Maybe you have a date/time for the last entrance and exit so you simply have to check which is greater. Maybe you have something else. You designed it so you should know how to use it to determine what the last action was. IF the current action matches the last action, display the message.