How to implement a simple online auction system using PHP
In today's digital era, online auction systems are becoming more and more popular. Through this system, people can conduct commodity auctions on the Internet to achieve wider participation and higher benefits. This article will introduce how to use PHP to write a simple online auction system and give specific code examples.
First, we need to create a database to store auction-related information. In MySQL, you can use the following SQL statement to create a database named auction:
CREATE DATABASE auction;
Then, we need to create a table named items to store the auction item information . The table contains fields id (auction item ID), name (auction item name), description (auction item description), start_price (starting price), current_price (current price), end_time (end time), etc. The table can be created using the following SQL statement:
CREATE TABLE items (
id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, start_price INT NOT NULL, current_price INT NOT NULL, end_time DATETIME NOT NULL
);
Next, we need to write PHP code to implement the functionality of the auction system. First, we need to connect to the database. You can connect to the database using the following code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "auction";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
?>
Then, we can create a file named index.php to display the auction item list and auction information. In this file, we need to query the database to obtain all auction item information and display it on the page. This can be achieved using the following code:
$sql = "SELECT * FROM items";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { echo "拍卖品ID: " . $row["id"]. " - 拍卖品名称: " . $row["name"]. " - 当前价: " . $row["current_price"]. "<br>"; }
} else {
echo "暂无拍卖品";
}
?>
Next, we can Create a file named auction.php to implement users' bidding operations on auction items. In this file, we need to get the bidding price entered by the user and update the current price in the database. This can be achieved using the following code:
if ($_SERVER["REQUEST_METHOD"] == "POST") { // Check whether the request method is POST
$item_id = $_POST["item_id"]; $bid_price = $_POST["bid_price"]; $sql = "SELECT * FROM items WHERE id = {$item_id}"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); if ($bid_price > $row["current_price"]) { $sql = "UPDATE items SET current_price = {$bid_price} WHERE id = {$item_id}"; if ($conn->query($sql) === TRUE) { echo "竞拍成功"; } else { echo "竞拍失败: " . $conn->error; } } else { echo "出价过低"; } } else { echo "拍卖品不存在"; }
}
?>
Finally, we need to create a file named bid.php to implement the user's bidding interface for a certain auction item. In this file, we need to use a form to receive the user's bid and send it to auction.php for processing. This can be achieved using the following code: