You update Your stock count after user proceed to checkout not when he adds it to cart. And put it back in stock if payment was not made in a week or some other time You choose.
Plus You check on each request or when he gets to cart page if all items are still available, maybe someone bought it in the meantime, and inform user about it.
But if You insist on updating stock when adding to cart then You will have to keep cart content in db and keep track of user activity by updating timestamp of last request. You do a cronjob that checks every x minutes for inactive users and puts back items in stock. You could add some automatic ajax request on page so if user didn't close browser but is afk to just keep him active.