Home >Backend Development >PHP Tutorial >How to use PHP to implement a campus second-hand trading platform
With the richness of university life and the increase in material needs, the campus second-hand trading platform has become an indispensable part of campus life. As a developer, how to use PHP to implement a campus second-hand trading platform is a skill we need to master. In this article, we will introduce how to use PHP to implement a campus second-hand trading platform, including database design, back-end management, front-end design, etc.
Database design is an essential step before building any website. In the campus second-hand trading platform, we need to define basic concepts such as users, products and transactions. To implement these concepts, we can use MySQL database to store data. The following are some tables we need to create:
User information table:
CREATE TABLE user
(
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `phone` varchar(255) NOT NULL, `avatar` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
Product information table:
CREATE TABLE item
(
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `title` varchar(255) NOT NULL, `description` text NOT NULL, `price` decimal(10,2) NOT NULL, `category` varchar(255) NOT NULL, `seller_id` int(11) NOT NULL, `sold` tinyint(1) NOT NULL DEFAULT '0', `create_time` datetime NOT NULL, `update_time` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
User Transaction table:
CREATE TABLE transaction
(
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `buyer_id` int(11) NOT NULL, `seller_id` int(11) NOT NULL, `item_id` int(11) NOT NULL, `price` decimal(10,2) NOT NULL, `create_time` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In the background management, we need to add, edit and delete products, manage user information and transaction information. We can use PHP to write the background management function and connect it to the database.
In PHP, we can use PDO or mysqli API to connect to the MySQL database. The following is an example using the mysqli API:
8197499e6c47f5995139ba46566fba88connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
In the background In the management interface, we can use PHP to write appropriate forms and handlers. For example, we can use the following code to add a product:
06df6657463e1d212f67dc77ed54e959 connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST ['price'];
$category = $_POST['category'];
$seller_id = $_POST['seller_id'];
$sql = "INSERT INTO item (title, description, price, category, seller_id) VALUES ('$title', '$description', '$price', '$category', '$seller_id')";
if ($conn-> query($sql) === TRUE) {
echo "New item added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
In front-end design, we need to consider the user interface and interaction. Use HTML, CSS, and JavaScript to implement a good user interface and provide users with a smooth experience.
Here are the HTML and CSS styles for a well-designed user login form:
8b05045a5be5764f313ed5b9168a17e6
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
<meta charset="UTF-8"> <title>Login - Campus Marketplace</title> <style> body { font-family: Arial, sans-serif; background-color: #f2f2f2; margin: 0; padding: 0; } .container { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background-color: #fff; padding: 20px; border-radius: 5px; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4); } h1 { font-size: 24px; margin: 0 0 20px 0; text-align: center; } form { display: flex; flex-direction: column; align-items: center; } input[type=text], input[type=password] { font-size: 16px; padding: 8px; margin-bottom: 10px; border-radius: 5px; border: none; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4); width: 100%; box-sizing: border-box; } input[type=submit] { background-color: #4CAF50; color: white; font-size: 16px; padding: 12px; border: none; border-radius: 5px; cursor: pointer; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4); } input[type=submit]:hover { background-color: #3e8e41; } </style>
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
<div class="container"> <h1>Login</h1> <form action="login.php" method="POST"> <input type="text" name="username" placeholder="Username"> <input type="password" name="password" placeholder="Password"> <input type="submit" value="Login"> </form> </div>
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
In JavaScript, we can use XMLHttpRequest Or fetch API to interact with the background and update page content.
Conclusion:
In this technical article, we introduced how to use PHP to implement the basic functions of the campus second-hand trading platform. We learned how to design a MySQL database, develop back-end management and design front-end user interface. By mastering these technologies, we can build a complete campus second-hand trading platform and provide campus students with a convenient and safe trading platform.
The above is the detailed content of How to use PHP to implement a campus second-hand trading platform. For more information, please follow other related articles on the PHP Chinese website!