Home > Article > Backend Development > How to write a simple online review system through PHP
How to write a simple online review system through PHP
Introduction:
With the development of the Internet, people have an increasing demand for online shopping and restaurant reviews. The bigger. This article will introduce how to write a simple online review system through PHP to meet the needs of users. The article will include the overall design of the system and specific PHP code examples.
1. Overall system design:
User table (user):
Product table (item):
Review form (review):
2. Specific code examples:
//Database connection code
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Determine whether the form is submitted
if(isset($_POST['submit'] )){
// 提取表单数据 $username = $_POST['username']; $password = $_POST['password']; // 执行插入数据库操作 $sql = "INSERT INTO user (username, password) VALUES ('$username', '$password')"; mysqli_query($conn, $sql); // 注册成功跳转到登录页面 header('Location: login.php'); exit();
}
?>
// Database connection Code
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Determine whether the form is submitted
if(isset($_POST['submit '])){
// 提取表单数据 $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){ // 登录成功跳转到首页 header('Location: index.php'); exit(); } else { // 登录失败提示错误信息 echo "用户名或密码错误"; }
}
?>
// Database connection code
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Query product data
$sql = "SELECT * FROM item";
$result = mysqli_query($conn, $sql);
// Loop to display product information
while($row = mysqli_fetch_assoc($result)){
echo "商品名称:" . $row['item_name'] . "<br>"; echo "商品描述:" . $row['item_description'] . "<br>"; // 提交点评表单 echo "<form action='review.php' method='post'>"; echo "<input type='hidden' name='item_id' value='" . $row['item_id'] . "'>"; echo "<textarea name='review_content'></textarea><br>"; echo "<input type='submit' name='submit' value='提交点评'>"; echo "</form>";
}
?>
// Database connection code
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Determine whether the form is submitted
if(isset($_POST['submit'])){
// 提取表单数据 $item_id = $_POST['item_id']; $review_content = $_POST['review_content']; $user_id = $_SESSION['user_id']; // 使用SESSION获取当前用户ID // 执行插入数据库操作 $sql = "INSERT INTO review (review_content, review_time, user_id, item_id) VALUES ('$review_content', NOW(), '$user_id', '$item_id')"; mysqli_query($conn, $sql); // 提交成功跳转回商品页面 header('Location: item.php?id=' . $item_id); exit();
}
?>
//Database connection code
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Extract URL parameters
$item_id = $_GET['id'];
// Query review data
$sql = "SELECT * FROM review WHERE item_id='$item_id'";
$result = mysqli_query($conn, $sql);
// Display review information in a loop
while($row = mysqli_fetch_assoc($result)){
echo "点评内容:" . $row['review_content'] . "<br>"; echo "点评时间:" . $row['review_time'] . "<br>";
}
?>
Conclusion:
Through the above PHP Code example, we can write a simple online review system. Of course, the system still has many functions that can be further improved and expanded, such as user rights management, comment like and comment functions, etc. I hope this article can help you understand how to write an online review system through PHP.
The above is the detailed content of How to write a simple online review system through PHP. For more information, please follow other related articles on the PHP Chinese website!