Home  >  Article  >  Backend Development  >  Second-hand recycling website uses PHP to develop one-click product details function

Second-hand recycling website uses PHP to develop one-click product details function

WBOY
WBOYOriginal
2023-07-01 15:51:111110browse

Second-hand recycling website uses one-click viewing of product details function developed by PHP

In recent years, the prevalence of consumerism has led to over-consumption and waste of goods. In order to reduce the waste of resources and environmental pollution, more and more people are beginning to choose second-hand recycling and reuse to deal with their idle items. In order to meet this demand, many second-hand recycling websites have emerged and, through the power of the Internet, allow people to easily post and browse unused items.

In second-hand recycling websites, a key feature is the ability to quickly view detailed information about the product. This article will use PHP code examples to show readers how to use the one-click product details function.

First, we need to create a database to store product information. In this example, we assume that the database name is "products" and the data table name is "items". The data table contains columns "id", "name", "description", "price" and "image", which correspond to the unique identifier, name, description, price and image of the product respectively.

Next, we need to create a page to display product details. In this example, we name the page "item.php". First, we need to get the unique identifier of the product on the page. We can pass this unique identifier through URL parameters. The code is as follows:

<?php
if(isset($_GET['id'])){
    $item_id = $_GET['id'];
}else{
    echo "商品不存在";
    exit;
}
?>

After obtaining the unique identifier of the product, we can use it to query the database and obtain detailed information about the product. The code is as follows:

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

// 查询商品的详细信息
$sql = "SELECT * FROM items WHERE id = '$item_id'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
    $row = mysqli_fetch_assoc($result);
    $name = $row['name'];
    $description = $row['description'];
    $price = $row['price'];
    $image = $row['image'];
}else{
    echo "商品不存在";
    exit;
}
?>

Then, we can display the product details on the page. The code is as follows:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $name; ?> - 商品详情</title>
</head>
<body>
    <h1><?php echo $name; ?></h1>
    <img src="<?php echo $image; ?>" alt="<?php echo $name; ?>">
    <p><?php echo $description; ?></p>
    <p>价格:<?php echo $price; ?></p>
</body>
</html>

Finally, we need to add a link to the list page of the second-hand recycling website so that users can click on the product name and jump to the product details page. The code is as follows:

<a href="item.php?id=<?php echo $item_id; ?>"><?php echo $name; ?></a>

Through the above code example, we can implement a simple one-click function to view product details. When the user clicks on the product name, it will jump to the product details page and display the detailed information of the product.

To sum up, in order to facilitate users to view product details, the second-hand recycling website has developed a one-click function to view product details using PHP. Through database query and page display, users can easily obtain product name, description, price, pictures and other information. The implementation of this function increases the user experience and functional integrity of the website, allowing users to browse and select second-hand items more conveniently.

The above is the detailed content of Second-hand recycling website uses PHP to develop one-click product details function. 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