Code generation for supplier management functions in PHP inventory management system

PHPz
Release: 2023-08-08 20:56:02
Original
1196 people have browsed it

Code generation for supplier management functions in PHP inventory management system

Code generation for supplier management function in PHP inventory management system

When developing an inventory management system, supplier management function is a very important part . This function is mainly used to record, manage and update supplier-related information, including the supplier's name, contact information, address and products associated with it. In this article, we will generate a simple supplier management function through PHP code to facilitate developers to apply it in actual projects.

First, we need to create a database, which contains two tables: vendors and products. The supplier table is used to store supplier-related information, and the product table is used to store product information related to suppliers. The two tables are related by vendor ID (vendor_id). Here is an example of the structure of the database:

vendors table:
- vendor_id (int, primary key)
- vendor_name (varchar)
- vendor_contact (varchar)
- vendor_address (varchar)

products table:
- product_id (int, primary key)
- product_name (varchar)
- vendor_id (int, foreign key)
- product_price (decimal)
Copy after login

Next, we can create a PHP file called vendor.php that will handle vendor-related operations. In this file, we will include code for functionality to add, edit, delete suppliers, and list all suppliers. The following is a code example for vendor.php:

<?php

// 数据库连接
$conn = new mysqli("localhost", "用户名", "密码", "数据库名");
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 添加供应商
if (isset($_POST["add_vendor"])) {
    $vendorName = $_POST["vendor_name"];
    $vendorContact = $_POST["vendor_contact"];
    $vendorAddress = $_POST["vendor_address"];

    $sql = "INSERT INTO vendors (vendor_name, vendor_contact, vendor_address) VALUES ('$vendorName', '$vendorContact', '$vendorAddress')";

    if ($conn->query($sql) === TRUE) {
        echo "供应商添加成功";
    } else {
        echo "供应商添加失败: " . $conn->error;
    }
}

// 编辑供应商
if (isset($_POST["edit_vendor"])) {
    $vendorId = $_POST["vendor_id"];
    $vendorName = $_POST["vendor_name"];
    $vendorContact = $_POST["vendor_contact"];
    $vendorAddress = $_POST["vendor_address"];

    $sql = "UPDATE vendors SET vendor_name='$vendorName', vendor_contact='$vendorContact', vendor_address='$vendorAddress' WHERE vendor_id='$vendorId'";

    if ($conn->query($sql) === TRUE) {
        echo "供应商编辑成功";
    } else {
        echo "供应商编辑失败: " . $conn->error;
    }
}

// 删除供应商
if (isset($_GET["delete_vendor"])) {
    $vendorId = $_GET["delete_vendor"];

    $sql = "DELETE FROM vendors WHERE vendor_id='$vendorId'";

    if ($conn->query($sql) === TRUE) {
        echo "供应商删除成功";
    } else {
        echo "供应商删除失败: " . $conn->error;
    }
}

// 列出所有供应商
$sql = "SELECT * FROM vendors";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "供应商ID: " . $row["vendor_id"]. " - 名称: " . $row["vendor_name"]. " - 联系方式: " . $row["vendor_contact"]. " - 地址: " . $row["vendor_address"]. "<br>";
    }
} else {
    echo "暂无供应商";
}

$conn->close();

?>
Copy after login

In the above code, we first establish a connection to the database. We then perform the corresponding action by determining whether the user clicked the button to add, edit, or delete a supplier. For the add supplier and edit supplier operations, we will get the relevant information from the user's input and insert or update it into the database through SQL statements. For the delete supplier operation, we will obtain the supplier ID from the URL parameter and delete the corresponding supplier information from the database based on this ID. Finally, we get the data of all suppliers by querying the suppliers table and list them.

In addition, in the front-end interface, we can use HTML forms and buttons to interact with the vendor.php file. Through the input boxes and buttons in the form, users can enter the name, contact information and address of the supplier, and add, edit or delete the corresponding supplier information. The following is an example of HTML code for a simple sample vendor management interface:

<!DOCTYPE html>
<html>
<head>
    <title>供应商管理系统</title>
</head>
<body>

<h1>供应商管理</h1>

<form action="vendor.php" method="post">
    <input type="text" name="vendor_name" placeholder="供应商名称" required><br>
    <input type="text" name="vendor_contact" placeholder="联系方式" required><br>
    <input type="text" name="vendor_address" placeholder="地址" required><br>
    <input type="submit" name="add_vendor" value="添加供应商">
</form>

<hr>

<h2>供应商列表</h2>

<?php require_once "vendor.php"; ?>

</body>
</html>
Copy after login

In the above HTML code, we have created a form for adding vendors and display the vendors by introducing the vendor.php file list of suppliers.

To summarize, through the above code examples, we can quickly implement a simple supplier management function and apply it to the inventory management system. Of course, based on actual needs, we can also optimize the code and add more functions, such as searching for suppliers, displaying suppliers in paging, etc. I hope the code examples provided in this article will be helpful to you in developing supplier management functions in your PHP inventory management system.

The above is the detailed content of Code generation for supplier management functions in PHP inventory management system. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template