Erstellen Sie Tabelle, wenn nicht existiert Inventory_logs ( ID int (11) nicht null auto_increment, product_id int (11) nicht null, Aktion varchar (255) nicht null, Menge int (11) nicht null, erstellte_at timestamp nicht null Standard current_timestamp, Primärschlüssel (ID) ) Engine = InnoDB Standard charSet = utf8;
Diese Tabelle enthält die folgenden Felder:
ID : Eine eindeutige Kennung für die Lagerung der Bestandsprotokollierung unter Verwendung eines selbstwachsenden Primärschlüssels.
product_id : Die Produkt -ID, verwendet, um bestimmte Produkte zu assoziieren.
Aktion : Inventarbetriebstypen wie im Geschäft, Out-Store usw.
Menge : Die Anzahl der Operationen, die die Anzahl der Produkte für den Betrieb angibt.
erstellt_at : zeichnet den erstellten Zeitstempel auf.
KlasseninventoryLog { privat $ pdo; Öffentliche Funktion __construct (PDO $ PDO) { $ this-> pdo = $ pdo; } Public Function Addlog ($ product_id, $ action, $ quantity) { $ sql = "Inventory_logs (product_id, Aktion, Menge) einfügen. Werte (: product_id ,: Aktion,: Menge) "; $ stmt = $ this-> pdo-> vorbereiten ($ sql); $ stmt-> bindValue (': product_id', $ product_id, pdo :: param_int); $ stmt-> bindValue (': action', $ action, pdo :: param_str); $ stmt-> bindValue (': Quantität', $ Quantität, pdo :: param_int); $ stmt-> execute (); } Öffentliche Funktion getLogs ($ product_id) { $ sql = "Select * aus inventory_logs wobei product_id =: product_id order by erstellt_at desc"; $ stmt = $ this-> pdo-> vorbereiten ($ sql); $ stmt-> bindValue (': product_id', $ product_id, pdo :: param_int); $ stmt-> execute (); return $ stmt-> fetchall (pdo :: fetch_assoc); } }
$ dbhost = 'localhost'; $ dbname = 'Inventory'; $ dbuser = 'root'; $ dbpassword = 'Passwort'; $ dsn = "mysql: host = $ dbHost; dbname = $ dbname; charset = utf8"; $ pdo = new pdo ($ dsn, $ dbuser, $ dbpassword); $ inventorylog = new InventoryLog ($ pdo); // Fügen Sie einen Inventor-Protokolldatensatz $ InventoryLog-> Addlog (1, 'Inventory', 10) hinzu; // Erhalten Sie das Inventarprotokoll von Produkt 1 $ logs = $ InventoryLog-> getLogs (1); foreach ($ protokolle als $ log) { Echo "Product ID:". $ log ['product_id']. "Betriebstyp:". $ log ['Aktion']. "Anzahl der Operationen:". $ log ['Quantität']. "<br> "; }