Home >Backend Development >PHP Tutorial >How to use PHP to write an automated product inventory counting tool
How to use PHP to write an automated product inventory counting tool
Inventory management is one of the important issues faced by every enterprise. In order to ensure accurate inventory and improve work efficiency, automated commodity inventory counting tools have become the first choice for business managers. This article will introduce how to use PHP to write a simple and practical automated product inventory counting tool, and attach relevant code examples.
$hostname = "localhost"; $username = "root"; $password = "password"; $database = "inventory"; $conn = new mysqli($hostname, $username, $password, $database); if ($conn->connect_error) { die("连接数据库失败:" . $conn->connect_error); }
$sql = "SELECT * FROM products"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $product_id = $row['id']; $product_name = $row['name']; $stock_qty = $row['stock_qty']; // 写入相应的处理逻辑 } } else { echo "没有查询到商品信息。"; }
$report = array(); while ($row = $result->fetch_assoc()) { $product_id = $row['id']; $product_name = $row['name']; $stock_qty = $row['stock_qty']; $counted_qty = // 根据实际情况填写盘点数量; if ($stock_qty != $counted_qty) { $report[] = array( 'product_id' => $product_id, 'product_name' => $product_name, 'stock_qty' => $stock_qty, 'counted_qty' => $counted_qty ); } } // 生成报告 if (!empty($report)) { foreach ($report as $item) { echo "商品编号:" . $item['product_id'] . " "; echo "商品名称:" . $item['product_name'] . " "; echo "实际库存数量:" . $item['stock_qty'] . " "; echo "盘点数量:" . $item['counted_qty'] . " "; } } else { echo "库存盘点结果一致,没有差异。"; }
if (!empty($report)) { echo "<table>"; echo "<tr><th>商品编号</th><th>商品名称</th><th>实际库存数量</th><th>盘点数量</th></tr>"; foreach ($report as $item) { echo "<tr>"; echo "<td>" . $item['product_id'] . "</td>"; echo "<td>" . $item['product_name'] . "</td>"; echo "<td>" . $item['stock_qty'] . "</td>"; echo "<td>" . $item['counted_qty'] . "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "库存盘点结果一致,没有差异。"; }
Through the above steps, we have successfully written a simple and practical automated product inventory counting tool. Obtain product information and inventory quantity by connecting to the database, take inventory and generate corresponding reports, and finally customize the report format according to needs. Further functional expansion can be carried out according to actual needs, such as adding permission management, exporting reports, etc.
I hope this article will help you understand how to use PHP to write an automated product inventory counting tool!
The above is the detailed content of How to use PHP to write an automated product inventory counting tool. For more information, please follow other related articles on the PHP Chinese website!