存在するしない場合はテーブルを作成しますinventory_logs( id int(11)null auto_incrementではない、 Product_id int(11)nullではありません、 アクションvarchar(255)nullではなく、 量int(11)nullではなく、 created_atタイムスタンプnullデフォルトcurrent_timestamp、 ホストキー(ID) )Engine = InnoDBデフォルトcharset = utf8;
このテーブルには、次のフィールドが含まれています。
ID :自己成長プライマリキーを使用した在庫ロギングの一意の識別子。
Product_id :特定の製品を関連付けるために使用される製品ID。
アクション:店内、店舗などの在庫操作タイプ。
数量:操作の数。操作の製品数を示します。
created_at :作成されたタイムスタンプを記録します。
クラスInventoryLog { プライベート$ PDO; パブリック関数__construct(pdo $ pdo) { $ this-> pdo = $ pdo; } パブリック関数addlog($ product_id、$ action、$量) { $ sql = "Inventory_logsに挿入(product_id、action、量) VALUES(:Product_id、:action、:Quantion) "; $ stmt = $ this-> pdo-> prepare($ sql); $ stmt-> bindvalue( ':product_id'、$ product_id、pdo :: param_int); $ stmt-> bindvalue( ':action'、$ action、pdo :: param_str); $ stmt-> bindvalue( ':量'、$量、pdo :: param_int); $ stmt-> execute(); } パブリック関数getlogs($ product_id) { $ sql = "select * from inventory_logs where product_id =:created_at desc"; $ stmt = $ this-> pdo-> prepare($ sql); $ stmt-> bindvalue( ':product_id'、$ product_id、pdo :: param_int); $ stmt-> execute(); $ stmt-> fetchall(pdo :: fetch_assoc)を戻るします。 } }
$ dbhost = 'localhost'; $ dbname = 'Inventory'; $ dbuser = 'root'; $ dbpassword = 'password'; $ dsn = "mysql:host = $ dbhost; dbname = $ dbname; charset = utf8"; $ pdo = new PDO($ dsn、$ dbuser、$ dbpassword); $ inventorylog = new InventoryLog($ PDO); //インベントリログレコードを追加します$ inventorylog-> addlog(1、 'inventory'、10); //製品のインベントリログを得る1 $ logs = $ inventorylog-> getLogs(1); foreach($ logs as $ log){ エコー「製品ID:」。 $ log ['product_id']。 「動作しますタイプ:」。 $ log ['Action']。 「動作します数:」。 $ log ['quanty']。 「<br> "; }