Incrementing or decrementing

klein9 -  
heliconius Posted messages 584 Status Membre -
bonjour!!!! j'aurais encore besoin d'aide SVP!!! j'aimerai savoir comment calculer la quantité finale en stock. C'est-à-dire de savoir quels matériels sont disponible en stock. Il s'agit de faire la quantité entrée moins la quantité sortie ce qui doit me donner la quantité finale. Et je ne sais pas comment écrire le code en php. Dans ma base de données j'ai une table MOUVEMENTS(CODE_MAT, QUANTITE_ENTREE, QUANTITE_SORTIE, DATE) la clé étrangère est CODE_MAT. C'est dans cette table MOUVEMENTS qu'on enregistre les entrées et les sorties. J'ai également une table STOCK (CODE_MAT , QUANTITE) dans laquelle on retrouve tout le matériel disponible en stock.

2 réponses

watarux Posted messages 324 Status Membre 42
 
Good evening, I'm here to help you and shed some light.

For my example, I created a database with 2 tables, the first one named goods

with fields

id, product, nb_entry, nb_exit.

and the second one named stock

with fields

id, product, stock

-----------------------

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
//Here we connect to the database
$DB = new PDO('mysql:host=localhost;dbname=test_ccm','root','');

//I create a query and ask it to get all the info where there is a product named keyboard
$sql = $DB->prepare("SELECT * FROM marchandises WHERE produit='clavier'");
//I execute the query
$sql->execute();
//I store in an array the result of my query
$tab = $sql->fetchall();

//I loop through this array
foreach($tab as $value)
{
//I perform the operation of the number of entries and exits of my product
$stock = $value['nb_entree'] - $value['nb_sortir'];
//I retrieve the name of the product in question
$name = $value['produit'];

//I perform another query to insert into a stock table the name of my product and the number of stock left
$sql = $DB->prepare("INSERT INTO `stock`(`id`, `produit`, `nb_stock`) VALUES ('',:name,:stock)");
$sql->execute(array(
':name' => $name,
':stock' => $stock
));
}

?>
</body>
</html>

It works, you can adapt it to your project.
0