Home > Article > Backend Development > How to use PHP and Vue to implement the loss reporting function of warehouse management
How to use PHP and Vue to implement the loss management function of warehouse management
In the warehouse management system, loss management is a crucial function, which can Help companies promptly detect and handle damaged goods in warehouses, reduce losses and improve efficiency. In this article, we will introduce how to use PHP and Vue to implement the loss reporting management function in the warehouse management system, and provide specific code examples to help readers better understand and apply.
First, we need to set up a basic environment to run our code. We will use PHP as the back-end language, Vue as the front-end framework, and MySQL as the database to implement various functions of loss management. Please make sure you have installed PHP, Vue and MySQL, and configured the relevant environment.
First, we need to create a database table to store loss report information. We create a table named "damage", which contains the following fields:
You can use the following SQL statement to create this table:
CREATE TABLE damage ( id INT(11) AUTO_INCREMENT PRIMARY KEY, product_id INT(11) NOT NULL, quantity INT(11) NOT NULL, date DATE NOT NULL, reason TEXT );
Next, we need to create a PHP file to handle the addition, query, modification and deletion of loss report information. We will create a file named "damage.php" and implement the following functions in the file:
<?php header('Content-Type: application/json'); // 连接到数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 查询所有报损记录 $result = $conn->query("SELECT * FROM damage"); // 将结果转换为JSON格式并返回 echo json_encode($result->fetch_all(MYSQLI_ASSOC)); // 关闭数据库连接 $conn->close(); ?>
<?php header('Content-Type: application/json'); // 获取POST请求的参数 $product_id = $_POST['product_id']; $quantity = $_POST['quantity']; $date = $_POST['date']; $reason = $_POST['reason']; // 连接到数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 插入报损记录 $conn->query("INSERT INTO damage (product_id, quantity, date, reason) VALUES ('$product_id', '$quantity', '$date', '$reason')"); // 返回成功的响应 echo json_encode(['status' => 'success']); // 关闭数据库连接 $conn->close(); ?>
<?php header('Content-Type: application/json'); // 获取POST请求的参数 $id = $_POST['id']; $quantity = $_POST['quantity']; $reason = $_POST['reason']; // 连接到数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 更新报损记录 $conn->query("UPDATE damage SET quantity='$quantity', reason='$reason' WHERE id='$id'"); // 返回成功的响应 echo json_encode(['status' => 'success']); // 关闭数据库连接 $conn->close(); ?>
<?php header('Content-Type: application/json'); // 获取POST请求的参数 $id = $_POST['id']; // 连接到数据库 $conn = new mysqli('localhost', 'username', 'password', 'database'); // 删除报损记录 $conn->query("DELETE FROM damage WHERE id='$id'"); // 返回成功的响应 echo json_encode(['status' => 'success']); // 关闭数据库连接 $conn->close(); ?>
Next, we need to use Vue to create a front-end interface to display the loss report record and provide the functions of adding, editing and deleting. We will use Vue's component development to create a component named "DamageManagement.vue", which contains the following functions:
The following is a simple code example:
<template> <div> <table> <thead> <tr> <th>ID</th> <th>商品ID</th> <th>报损数量</th> <th>报损日期</th> <th>报损原因</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="record in records" :key="record.id"> <td>{{ record.id }}</td> <td>{{ record.product_id }}</td> <td>{{ record.quantity }}</td> <td>{{ record.date }}</td> <td>{{ record.reason }}</td> <td> <button @click="edit(record)">编辑</button> <button @click="delete(record.id)">删除</button> </td> </tr> </tbody> </table> <form v-if="mode === 'add' || mode === 'edit'" @submit.prevent="submit"> <div> <label>商品ID</label> <input type="text" v-model="product_id" required> </div> <div> <label>报损数量</label> <input type="number" v-model="quantity" required> </div> <div> <label>报损日期</label> <input type="date" v-model="date" required> </div> <div> <label>报损原因</label> <textarea v-model="reason" required></textarea> </div> <button type="submit">{{ mode === 'edit' ? '保存' : '添加' }}</button> </form> </div> </template> <script> export default { data() { return { records: [], mode: "view", product_id: "", quantity: "", date: "", reason: "", selectedRecord: null, }; }, created() { this.fetchRecords(); }, methods: { fetchRecords() { // 发起请求获取所有的报损记录 // ... // 将结果存储到records中 // this.records = ... }, add() { // 切换到添加模式 this.mode = "add"; }, edit(record) { // 切换到编辑模式,并将选中的记录赋值给selectedRecord this.mode = "edit"; this.selectedRecord = record; this.product_id = record.product_id; this.quantity = record.quantity; this.date = record.date; this.reason = record.reason; }, delete(id) { // 发起请求删除指定ID的报损记录 // ... }, submit() { if (this.mode === "add") { // 发起请求添加报损记录 // ... } else if (this.mode === "edit") { // 发起请求更新指定ID的报损记录 // ... } // 切换回查看模式,并重置表单数据 this.mode = "view"; this.selectedRecord = null; this.product_id = ""; this.quantity = ""; this.date = ""; this.reason = ""; }, }, }; </script>
I hope that through this example, readers can better understand and apply PHP and Vue to implement the loss reporting management function in the warehouse management system. Of course, in actual application, appropriate modifications and adjustments need to be made according to specific business needs. The implementation of some functions in the code example is just a simple example, and some other factors need to be considered during the actual development process, such as data verification, permission control, etc. I hope this article can be helpful to readers, thank you for reading!
The above is the detailed content of How to use PHP and Vue to implement the loss reporting function of warehouse management. For more information, please follow other related articles on the PHP Chinese website!