Hello,
As a principle, we *
never* store data that can be calculated. It seems to me (I'm being cautious as I don't know the problem and haven't seen the MCD) that the Stock table is unnecessary because it stores data that can be calculated. Doing so complicates things because during the restocking of materials, we would need to update BOTH the Movements table AND the Stock table, the same during material outputs. The Movements table is sufficient by itself.
Here is this table:
Movements(idmvt, code_mat, qte_entree, qte_sortie, date) // *idmvt* = primary key
+-------+----------+------------+------------+------------+
| idmvt | code_mat | qte_entree | qte_sortie | date_mvt |
+-------+----------+------------+------------+------------+
| 1 | 167 | 0 | 10 | 2014-10-08 |
| 2 | 45 | 100 | 0 | 2014-10-23 |
| 3 | 167 | 100 | 0 | 2014-10-23 |
| 4 | 1432 | 0 | 50 | 2014-11-02 |
| 5 | 167 | 50 | 0 | 2014-11-03 |
| 6 | 167 | 0 | 28 | 2014-11-10 |
function inStock($idmat) { global $connexion; // $connexion = database connection link $requete = "SELECT SUM(qte_entree) AS in, SUM(qte_sortie) AS out" . "FROM Mouvements " . "WHERE code_mat='$idmat';"; $resultat = ExecRequete($request, $connexion); // See below // +-------+-------+ // | in | out | // +-------+-------+ // | 150 | 38 | // +-------+-------+ // Retrieve the line in the returned result $r = mysql_fetch_object($resultat); return ($r->in - $r->out); // return 150-38 = 112 } In the code, to write the stock level of all materials:
$requete = "SELECT code_mat, libelle FROM Materiels ORDER BY libelle;"; $resultat = ExecRequete($requete, $connexion); echo "Stock Status :"; echo "<ul>\n"; while($r = mysql_fetch_object($resultat)) { echo "<li> $r->libelle : " . inStock($r->code_mat) . "</li>\n"; } echo "</ul>\n"; ExecRequete() :
---------------
For my part, I *always* use a file init.php that I systematically include at the beginning of each PHP page
<?php require('/lib/init.php'); ?><html> <head> etc... This init.php file contains:
- all the general variables to initialize,
- reading the site's configuration table (if it exists)
- the functions that we can frequently use in the pages
- the database connection
- query executions
- etc...
Regarding the database:
- definitions (USER, SERVER, ... )
- functions for connection, query execution with result or error return
<?php // File: init.php // Date: YYYY-MM-DD // Database connection info define (NOM, "user_mysql"); // MySQL User define (PASS, "password"); // Password define (SERVER, "000.111.222.333"); // Server name or IP define (BASE, "base"); // Database to use /* mysql> show tables; +--------------------+ | Tables_in_base | +--------------------+ | gia_Categories | | gia_Clients | | ic_ConfigSite | | etc... | +--------------------+ 38 rows in set (0.00 sec) */ // Table names // Scripts use TB_CAT, TB_CLI, etc... and not table names // If the table names need to change, it's unnecessary to rewrite all the // scripts. Just modify here. define (TB_CAT, "gia_Categories"); // Table of service categories define (TB_CLI, "gia_Clients"); // Table of clients // etc ... // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Various variables $cfg = "ic_ConfigSite"; // MySQL table containing the site's configuration, otherwise, comment $_debug = 0; // Should we be in debug mode (1) or not (0)? $_dmail = "mail@fai.com"; // Email address for debug mails $lastsave = 0; // Date of the last backup (timestamp) // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Display the error message and the query that generated it. function showSQLError($cnx,$sql) { $msg = "<b>Error in the query!</b><br>".mysql_error($cnx)."<br>"; $msg .= "<b>Query:</b><br>$sql<hr>"; return $msg; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Display a possible error message and exit function showError($error,$sql="") { $cnx = $GLOBALS["connexion"]; if($error == 1) { echo "Server unreachable!"; } if($error == 2) { echo "Database unreachable!"; } if($error == 3) { echo showSQLError($cnx,$sql); } exit; } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Executes a query and returns its result or an error message function ExecRequete($requete,$connexion) { $resultat = mysql_query($requete,$connexion); if($resultat) { return $resultat; } else { showError(3,$requete); } } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Connect to the server, then select the database if(!@$connexion = mysql_pconnect(SERVER,NOM,PASS)) { showError(1); } if(!@$ok = mysql_select_db(BASE,$connexion)) { showError(2); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Retrieve the site configuration // table ic_ConfigSite // +------+------------------------+ // | key | value | // +------+------------------------+ // | mail | webmaster@fai.com | // | tel | 01 02 03 04 05 | // | url | http://www.mysite.fr | // | img | /img | // | lib | /lib | if($cfg) { $requete = "SELECT * FROM $cfg;"; $config = ExecRequete($requete,$connexion); while($c = mysql_fetch_object($config)) { ${$c->clef} = $c->valeur; } // In pages use: $mail, $tel, $url, $img, $lib, etc... } ?> In the pages:
$requete = "SELECT ... FROM ... WHERE ...;";
$resultat = ExecRequete($requete, $connexion);
Example:
$requete = "SELECT " . TB_CLI.".nom, ".TB_CLI.".prenom, ". etc... . "FROM " . TB_CLI.", ".TB_CAT." " . "WHERE " . "condition 1 "; . "AND " . "condition 2;" . "ORDER BY " . TB_CLI.".nom, ".TB_CLI.".prenom;"; $resultat = ExecRequete($requete, $connexion); while($r = mysql_fetch_object($resultat)) { process the $r->query_columns... } A problem without a solution is a problem poorly posed. (Albert Einstein)