Home > Backend Development > PHP Tutorial > Second-hand recycling website developed using PHP supports online bidding function

Second-hand recycling website developed using PHP supports online bidding function

王林
Release: 2023-07-01 20:36:01
Original
836 people have browsed it

The second-hand recycling website developed using PHP supports online bidding function

As people's awareness of environmental protection and resource recycling and reuse increases, the second-hand recycling industry is gradually emerging. In order to facilitate users to sell second-hand items and provide them to others for use, it is very necessary to develop a second-hand recycling website that supports online bidding. This article will introduce how to use PHP to develop a second-hand recycling website with online bidding function, and attach relevant code examples.

1. Project preparation

Before we start, we need to do some project preparation work. First, we need to install a PHP development environment, which can be achieved by downloading and installing WAMP/MAMP/LAMP and other software packages. Secondly, we also need a database to store data such as user information and item information. This article will use the MySQL database.

2. Database design

In the design of this second-hand recycling website, we mainly need two database tables: user table and item table. The user table is used to store user registration information, including user name, password, email, etc. The item table is used to store second-hand item information posted by users, including item name, picture, description, bidding status, etc.

The following is an example of the design of the user table and item table:

User table (user):

CREATE TABLE `user` (
 `id` INT NOT NULL AUTO_INCREMENT,
 `username` VARCHAR(50) NOT NULL,
 `password` VARCHAR(50) NOT NULL,
 `email` VARCHAR(50) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

Item table (item):

CREATE TABLE `item` (
 `id` INT NOT NULL AUTO_INCREMENT,
 `title` VARCHAR(50) NOT NULL,
 `image` VARCHAR(100) NOT NULL,
 `description` TEXT NOT NULL,
 `price` DECIMAL(10,2) NOT NULL,
 `status` INT NOT NULL DEFAULT 0,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

三, Website function implementation

In this second-hand recycling website, we mainly implement the following functions: user registration, user login, item publishing, item list display, item bidding, etc.

  1. User Registration

The user registration function allows users to register by filling in their username, password and email address. During the registration process, you need to verify whether the user name and email address have been used. We can do this by querying the database.

The following is a simple user registration code example:

<?php
// 连接数据库
$conn = mysqli_connect("localhost","root","root","db_name");
// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

// 接收注册表单提交的数据
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];

// 验证用户名是否已存在
$sql = "SELECT * FROM user WHERE username='$username'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
    echo "用户名已存在";
    exit();
}

// 验证邮箱是否已存在
$sql = "SELECT * FROM user WHERE email='$email'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
    echo "邮箱已存在";
    exit();
}

// 将用户注册信息插入数据库
$sql = "INSERT INTO user (username, password, email) VALUES ('$username', '$password', '$email')";
if (mysqli_query($conn, $sql)) {
    echo "注册成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
Copy after login
  1. User login

The user login function allows users to log in by entering their username and password . After successfully logging in, you can further perform operations such as item publishing, item list display, and item bidding.

The following is a simple user login code example:

<?php
session_start();

// 连接数据库
$conn = mysqli_connect("localhost","root","root","db_name");
// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

// 接收登录表单提交的数据
$username = $_POST['username'];
$password = $_POST['password'];

// 验证用户名和密码是否匹配
$sql = "SELECT * FROM user WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
    // 登录成功,保存用户信息到session
    $_SESSION['username'] = $username;
    echo "登录成功";
} else {
    echo "用户名或密码错误";
}

mysqli_close($conn);
?>
Copy after login
  1. Item publishing

The item publishing function allows users to fill in the name, picture, and description of the item and starting price and other information, and save this information to the database.

The following is a simple item publishing code example:

<?php
session_start();

// 连接数据库
$conn = mysqli_connect("localhost","root","root","db_name");
// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

// 用户是否登录检查
if (!isset($_SESSION['username'])) {
    echo "请先登录";
    exit();
}

// 获取用户ID
$sql = "SELECT id FROM user WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$user_id = $row['id'];

// 接收物品发布表单提交的数据
$title = $_POST['title'];
$image = $_POST['image'];
$description = $_POST['description'];
$price = $_POST['price'];

// 将物品信息插入数据库
$sql = "INSERT INTO item (user_id, title, image, description, price) VALUES ($user_id, '$title', '$image', '$description', $price)";
if (mysqli_query($conn, $sql)) {
    echo "物品发布成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
Copy after login
  1. Item list display

The item list display function is used to display all published items Information, including the item's name, picture, description, current bidding price, etc.

The following is a simple item list display code example:

<?php
$conn = mysqli_connect("localhost","root","root","db_name");
// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

// 查询物品信息
$sql = "SELECT * FROM item";
$result = mysqli_query($conn, $sql);

// 输出物品列表
while($row = mysqli_fetch_assoc($result)) {
    echo "物品名称:" . $row['title'] . "<br>";
    echo "物品图片:<img src="" . $row['image'] . ""><br>";
    echo "物品描述:" . $row['description'] . "<br>";
    echo "当前竞价价格:" . $row['price'] . "<br><br>";
}

mysqli_close($conn);
?>
Copy after login
  1. Item bidding

The item bidding function allows users to bid on published items , the user can enter a bidding price and save it to the database.

The following is a simple item bidding code example:

<?php
session_start();

// 连接数据库
$conn = mysqli_connect("localhost","root","root","db_name");
// 检查连接是否成功
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}

// 用户是否登录检查
if (!isset($_SESSION['username'])) {
    echo "请先登录";
    exit();
}

// 获取用户ID
$sql = "SELECT id FROM user WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$user_id = $row['id'];

// 接收物品竞价表单提交的数据
$item_id = $_POST['item_id'];
$bid_price = $_POST['bid_price'];

// 更新物品竞价价格
$sql = "UPDATE item SET price=$bid_price WHERE id=$item_id";
if (mysqli_query($conn, $sql)) {
    echo "竞价成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
Copy after login

4. Summary

By using a second-hand recycling website developed with PHP, we have implemented user registration, user login, Functions such as item publishing, item list display, and item bidding. The above code example is just a simple implementation, you can modify and extend it according to actual needs. I hope this article will be helpful for learning PHP development and building a second-hand recycling website.

The above is the detailed content of Second-hand recycling website developed using PHP supports online bidding function. 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