Here we have firstly truncate our magento cart,
$cart = Mage::getModel('checkout/cart');
$cart->truncate()->save(); // remove all active items in cart page
$cart->init();
Mage::getSingleton('checkout/session')->clear();
After this, we tried to add products
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$cart->addProduct($id,$qty);
It's giving 500 Internal Server Error
Note: It's only giving error when we apply adding product quantity to max.
Ex. available qty. 100
here in my case $qty=100
Note : Our code is working perfectly but only the first time, the rest of the time it's giving this error.