I have this function that searches the database for the quantity of products in stock.
public function contarProduto() {
try {
$pdo = Conexao::getInstance();
$sql = "select id_produto as produto, sum(quantidade) as quantidade from estoque where tipo = 'entrada' group by id_produto;";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$saida = array();
$i = -1;
while ($rs = $stmt->fetch()) {
$i = $i + 1;
$saida[$i] = array("id" => ($rs['produto']), "qtd" => utf8_encode($rs['quantidade']));
}
return $saida[0];
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
And this page that transforms content into JSON
<?php
require_once 'Modelo/DAO/EstoqueDAO.php';
$estoque = new EstoqueDAO();
$jason = $estoque->contarProduto();
echo json_encode($jason);
?>
I have this code that makes an AJAX request only that returns all products, even passing a data
which in the case is id:1
var t = {id: 1};
$.getJSON("http://localhost/impressoras/newEmptyPHPWebPage.php",t, function
(d) {
console.log(d.qtd);
});
I need this request to return only the product that is sent by id
. Can someone help me?
In case the customer choose a product through a select, the quantity will automatically appear in the stock, so I can not pass the id through the sql.