<?php
session_start();
    header('Access-Control-Allow-Origin:*');
    header('Content-Type; application/json; charset=utf-8');
    header('Access-control-Allow-Method: POST');
    header('Access-control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-request-With');
    include('db.php');
        $inputdata = json_decode(file_get_contents("php://input"), true);
        
        $qte = $inputdata['qte'];
        $pid = $inputdata['pid'];
        $user = $inputdata['user'];
        
        $sel=$db->prepare('SELECT * FROM tbl_product WHERE pid=?');
        $sel->execute(array($pid));
        while($sels=$sel->fetch()){
            $produit=$sels['pname'];
        }
        

        $pub=$db->prepare('UPDATE tbl_product SET pstock=pstock+? WHERE pid=?');

        $pub->execute(array($qte,$pid));

        if ($pub) {
            $pubv=$db->prepare('INSERT INTO stock(user,produit,qte) VALUES(?,?,?)');

            $pubv->execute(array($user,$produit,$qte));
            
            if($pubv){
                 echo(json_encode(['response' => 1]));
            } else{
                 echo(json_encode(['response' => 0]));
            }
           
        } else{
             echo(json_encode(['response' => 0]));
        }
        
        