Here is the useful code for a purpose : update stock item quantity for a product in magento:

$product = Mage::getModel('catalog/product')->load($productId);

if ($product){

if (!($stockItem = $product->getStockItem())) {
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product);
}

$stockItem->setData('qty',intval($qty))
->setData('is_in_stock', intval($qty) > 0 ? 1 : 0)
->setData('manage_stock', 1)
->setData('use_config_manage_stock', 0)
->save();

}