Home  >  Article  >  Backend Development  >  PHP development to implement the product management function of the mall

PHP development to implement the product management function of the mall

PHPz
PHPzOriginal
2023-06-30 16:19:381525browse

In modern society, e-commerce has become one of the main ways for people to buy and sell goods. With the rapid development of e-commerce, more and more people are beginning to consider setting up their own online shopping malls to start a business. The product management function is a very important part of an online mall. It is related to the management and control of merchants' inventory, sales, logistics and other information. In this article, we will introduce how to use PHP to develop an online mall with powerful product management functions.

First, we need to build a database to store product-related information. You can use MySQL as the database management system and create a database named "product_management". Then, create a data table named "products" in the database, which will contain the following fields: product ID (product_id), product name (product_name), product description (product_description), unit price (price), inventory ( stock) etc.

Next, we need to write PHP code to implement the product management function. First, connect to the MySQL database and select the "product_management" database. Then, you can use SQL statements to create data tables. The following is a sample code:

<?php
// 定义数据库连接信息
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "product_management";

// 连接到数据库
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}

// 创建数据表
$sql = "CREATE TABLE products (
    product_id INT PRIMARY KEY AUTO_INCREMENT,
    product_name VARCHAR(255) NOT NULL,
    product_description TEXT,
    price DECIMAL(10, 2) NOT NULL,
    stock INT NOT NULL
)";

if ($conn->query($sql) === TRUE) {
    echo "数据表创建成功";
} else {
    echo "创建数据表失败:" . $conn->error;
}

$conn->close();
?>

Next, we need to write a product management page, on which products can be added, deleted, modified and queried. The following is the basic structure and sample code of the page:

<!DOCTYPE html>
<html>
<head>
    <title>商品管理</title>
</head>
<body>
    <h1>商品列表</h1>

    <table>
        <tr>
            <th>ID</th>
            <th>名称</th>
            <th>描述</th>
            <th>价格</th>
            <th>库存</th>
            <th>操作</th>
        </tr>

        <?php
        // 从数据库中获取商品列表
        $sql = "SELECT * FROM products";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // 将查询结果输出到页面上
            while($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row["product_id"] . "</td>";
                echo "<td>" . $row["product_name"] . "</td>";
                echo "<td>" . $row["product_description"] . "</td>";
                echo "<td>" . $row["price"] . "</td>";
                echo "<td>" . $row["stock"] . "</td>";
                echo "<td>
                        <a href='edit_product.php?id=" . $row["product_id"] . "'>编辑</a>
                        <a href='delete_product.php?id=" . $row["product_id"] . "'>删除</a>
                      </td>";
                echo "</tr>";
            }
        } else {
            echo "没有查询到商品";
        }
        ?>

    </table>

    <h1>添加商品</h1>
    <form action="add_product.php" method="post">
        <label>商品名称:</label>
        <input type="text" name="product_name" required><br>
        <label>商品描述:</label>
        <textarea name="product_description"></textarea><br>
        <label>单价:</label>
        <input type="text" name="price" required><br>
        <label>库存:</label>
        <input type="text" name="stock" required><br>
        <input type="submit" value="添加">
    </form>

</body>
</html>

The above code will display the product list on the page and provide the functions of adding, editing and deleting products. It should be noted that corresponding PHP files need to be written to handle these operations.

Through the above steps, we successfully implemented an online mall developed in PHP with product management functions. Merchants can add, delete, modify and check products through this system, and effectively manage their sales and inventory information. Of course, based on actual needs, we can also expand and optimize functions on this basis. I hope this article will be helpful to friends who want to develop a PHP online mall.

The above is the detailed content of PHP development to implement the product management function of the mall. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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