Home  >  Article  >  Backend Development  >  How to use PHP developer city function: Create product details page

How to use PHP developer city function: Create product details page

WBOY
WBOYOriginal
2023-07-30 13:45:101572browse

How to use PHP developer mall function: Create product details page

With the rapid development of e-commerce, developing a fully functional mall website has become a need for many developers. As one of the most important pages in a shopping mall website, the product details page needs to display detailed information about the product to attract users' attention and prompt them to purchase. This article will introduce how to use the product details page in the PHP developer city function and provide code examples.

Step 1: Create a database table

First, we need to create a database table to store product details. Suppose our database is named "myshop" and the table is named "products". The table contains the following fields: id (product ID), name (product name), price (product price), description (product description). The table can be created using the following SQL statement:

CREATE TABLE products (
    id INT(11) NOT NULL AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    description TEXT,
    PRIMARY KEY (id)
);

Step 2: Create an HTML template for the product details page

Before creating the product details page, we first create an HTML template as a basis. The following is a simple product details page HTML template example:

<!DOCTYPE html>
<html>
<head>
    <title>产品详情</title>
</head>
<body>
    <h1>产品详情</h1>
    <div>
        <h2 id="product-name"></h2>
        <p id="product-price"></p>
        <p id="product-description"></p>
    </div>
</body>
</html>

Step 3: Write PHP code to obtain product information

Next, we need to write PHP code to obtain product information in the database , and insert it into the HTML template. The following is a simple PHP code example:

<?php
    // 建立与数据库的连接
    $conn = mysqli_connect("localhost", "username", "password", "myshop");
    if (!$conn) {
        die("连接数据库失败: " . mysqli_connect_error());
    }

    // 获取产品ID
    $productId = $_GET["id"];

    // 查询产品信息
    $sql = "SELECT * FROM products WHERE id = $productId";
    $result = mysqli_query($conn, $sql);
    $product = mysqli_fetch_assoc($result);

    // 关闭数据库连接
    mysqli_close($conn);
?>

<!-- 在HTML模板中插入产品信息 -->
<script>
    window.onload = function() {
        document.getElementById("product-name").innerHTML = "<?php echo $product['name']; ?>";
        document.getElementById("product-price").innerHTML = "价格: <?php echo $product['price']; ?>";
        document.getElementById("product-description").innerHTML = "<?php echo $product['description']; ?>";
    };
</script>

In the above code, we first establish a connection to the database and then obtain the product ID (usually passed through URL parameters). Next, execute the query statement to obtain product information and store the results in the $product variable. Finally, close the database connection. In the HTML template, we use PHP's echo statement to dynamically insert product information into the corresponding HTML element.

Step 4: Test the product details page

After completing the above steps, we can access the product details page through the following URL:

http://yourdomain.com/product.php?id=1

Among them, id=1 represents the product ID. By modifying the value of the id parameter, we can view the details of different products.

Summary:

Through the above steps, we successfully created a PHP-based product details page. In actual development, we can expand this page according to needs, such as adding product pictures, user reviews and other functions. I hope this article will help you understand how to use the product details page of the PHP Developer City function. Remember, continuous learning and practice are the best ways to improve your development skills. Happy development!

The above is the detailed content of How to use PHP developer city function: Create product details page. 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