I don't see in code part when ps_stock_available is updated. You only update quantity column in ps_product table.
query will be like this but check it first
UPDATE ps_stock_available sa LEFT JOIN ps_product p ON (sa.id_product = p.id_product) SET sa.quantity = 'YOUR QUANTITY' WHERE p.reference = 'YOUR-REFERENCE-CODE'
Remember to escape slash for $reference in code. You dont need to slash escape quantity value
---------------------------------------------------------------------
Anyway You should try to use build in prestashop resource like
require_once(dirname(__FILE__).'/../../config/config.inc.php'); // path to config // somekind of foreach or loop, while etc and put the code below into this $product = new Product(1234); // id of product if (Validate::isLoadedObject($product)){ $product->quantity = 5; // your quantity $product->save(); }