In the past when I have made carts that need this feature I added a dropdown box for the quantity with the loop limit set to the amount set in the admin, you could then use a simple if routine that shows "sorry out of stock" if the item reaches 0
On the admin side you could also set the stock limit to "no limit" so in the front end instead of a drop down you get a text field again controlled by a simple if statement
Regarding stock, after the cart has been completed you can add an update that subtracts the amount ordered from the appropriate column as in ($stock = ($stock - $purchased_amount)) but to do this you will need to set a unique Id for each transaction so if they leave the site to go to a payment gateway when they return the correct items can be found to update