Home  >  Article  >  Backend Development  >  How to write a simple online review system through PHP

How to write a simple online review system through PHP

王林
王林Original
2023-09-24 09:25:02794browse

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:

  1. Database design:
    The online review system needs to store data such as user information, product information, and review information. Therefore, we need to design corresponding database tables to store this information. The following is a simple database table design example:

User table (user):

  • User ID (user_id)
  • User name (username)
  • Password (password)

Product table (item):

  • Product ID (item_id)
  • Product name (item_name)
  • Product description (item_description)

Review form (review):

  • Review ID (review_id)
  • Review content (review_content )
  • Review time(review_time)
  • User ID(user_id)
  • Product ID(item_id)
  1. System functions:
    The online review system needs to implement the following basic functions:
  2. User registration: Users can create new user accounts through the registration page.
  3. User login: Users can log in to the system using their registered accounts.
  4. Product display: The system needs to display all product information for users to comment.
  5. Comment submission: Users can submit comments on the product details page.
  6. Review list display: The system needs to display all review information for each product.

2. Specific code examples:

  1. User registration code:

//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();

}
?>

  1. User login code:

// 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 "用户名或密码错误";
}

}
?>

  1. Product display and review submission code:

// 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>";

}
?>

  1. Comment submission code:

// 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();

}
?>

  1. Comments list display code:

//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!

Statement:
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