Using PHP to develop a second-hand recycling website to support users to share reviews
With the gradual increase in environmental awareness, second-hand recycling has become a more popular way, which can not only reduce pressure on the environment, but also The value of useless items provided by users. In order to better serve users, the second-hand recycling website developed using PHP not only provides the function of commodity trading, but also supports users to share reviews and improve the user's purchasing experience. This article will introduce how to use PHP to develop such a website and provide relevant code examples.
Step one: Create database and tables
First, we need to create a database to store user evaluation information. In the MySQL database, you can use the following statement to create a table named "reviews":
CREATE TABLE reviews (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
user_id INT(11),
product_id INT(11),
rating INT(11),
comment TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
In this table, we have several Fields: id (ID used to uniquely identify the evaluation), user_id (ID of the evaluation user), product_id (ID of the evaluated product), rating (rating, which can be 1 to 5), comment (content of the evaluation), created_at ( the time the review was created).
Step 2: Display evaluation information
Next, we need to display evaluation information on the product details page. We can use the following code to get the evaluation information from the database and display it:
// Connect to the database
$mysqli = new mysqli('localhost', 'username ', 'password', 'database');
// Check whether the connection is successful
if ($mysqli->connect_errno) {
die('Failed to connect to the database:' . $mysqli ->connect_error);
}
// Get product ID
$product_id = $_GET['product_id'];
// Query evaluation information
$ query = "SELECT * FROM reviews WHERE product_id = '$product_id'";
$result = $mysqli->query($query);
// Display review information
while ($ row = $result->fetch_assoc()) {
echo '
Rating:' . $row['rating'] . '
';Evaluation content:' . $row['comment'] . '
';Evaluation time:' . $row[' created_at'] . '
';// Close the database connection
$mysqli->close() ;
?>
Step 3: Users add reviews
Finally, we need to provide a form for users to add reviews. Users can choose to rate and fill in the evaluation content, and click the submit button to save the evaluation information.
// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get user ID and product ID
$user_id = $_POST['user_id'];
$product_id = $_POST['product_id'];
// Get ratings and evaluation content
$rating = $_POST[' rating'];
$comment = $_POST['comment'];
//Connect to the database
$mysqli = new mysqli('localhost', 'username', 'password', ' database');
// Check whether the connection is successful
if ($mysqli->connect_errno) {
die('连接数据库失败:' . $mysqli->connect_error);
}
// Insert evaluation information
$query = "INSERT INTO reviews (user_id, product_id, rating, comment)
VALUES ('$user_id', '$product_id', '$rating', '$comment')";
if ($mysqli->query($query) === TRUE) {
echo '评价成功';
} else {
echo '评价失败:' . $mysqli->error;
}
//Close the database connection
$mysqli->close();
}
?>
< ;form action="" method="POST">
<option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option>
Through the above code example, we can implement a second-hand recycling website, and users can See other users' reviews on the product details page, and you can also add your own reviews. Such a function can not only improve users' purchasing trust, but also provide reference for other users to better meet users' needs.
Summary:
This article introduces how to use PHP to develop a second-hand recycling website that supports users to share reviews. We first created a database and table for storing review information, then displayed the review information on the product details page, and finally provided a form for users to add reviews. Through these functions, we can improve the user purchasing experience and make greater contributions to environmental protection.
The above is the detailed content of Second-hand recycling website developed using PHP supports users to share reviews. For more information, please follow other related articles on the PHP Chinese website!