Gitlab@Informatics

Skip to content
Snippets Groups Projects
Select Git revision
  • ef5639b34234595d5e2ae4955d935208833bc58f
  • master default
  • main protected
3 results

add_review.php

Blame
  • add_review.php 3.57 KiB
    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    include 'components/connect.php';
    
    // ตรวจสอบว่าผู้ใช้ล็อกอินหรือไม่
    if (!isset($user_id) || empty($user_id)) {
        die("Error: User is not logged in.");
    }
    
    // ตรวจสอบว่ามี get_id หรือไม่
    if(isset($_GET['get_id'])){
       $get_id = $_GET['get_id'];
    }else{
       header('location:all_posts.php');
       exit();
    }
    
    // ตรวจสอบว่าฟังก์ชัน create_unique_id() มีอยู่จริงหรือไม่
    if (!function_exists('create_unique_id')) {
        function create_unique_id() {
            return uniqid();
        }
    }
    
    // ตรวจสอบว่ามีการส่งแบบฟอร์มหรือไม่
    if(isset($_POST['submit'])) {
        $id = create_unique_id();
        $title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
        $description = filter_var($_POST['description'], FILTER_SANITIZE_STRING);
        $rating = filter_var($_POST['rating'], FILTER_SANITIZE_STRING);
    
        // ตรวจสอบว่าตาราง reviews มีอยู่หรือไม่
        $check_table = $conn->query("SHOW TABLES LIKE 'reviews'");
        if ($check_table->rowCount() == 0) {
            die("Error: Table 'reviews' does not exist in the database.");
        }
    
        // ตรวจสอบว่าผู้ใช้เคยรีวิวโพสต์นี้หรือไม่
        $verify_review = $conn->prepare("SELECT * FROM `reviews` WHERE post_id = ? AND user_id = ?");
        $verify_review->execute([$get_id, $user_id]);
    
        if ($verify_review->rowCount() > 0) {
            $warning_msg[] = 'Your review already added!';
        } else {
            $add_review = $conn->prepare("INSERT INTO `reviews` (id, post_id, user_id, rating, title, description) VALUES (?, ?, ?, ?, ?, ?)");
            $add_review->execute([$id, $get_id, $user_id, $rating, $title, $description]);
            $success_msg[] = 'Review added!';
        }
    }
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
       <meta charset="UTF-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>add review</title>
       <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
    
    <?php include 'components/header.php'; ?>
    
    <section class="account-form">
       <form action="" method="post">
          <h3>post your review</h3>
          <p class="placeholder">review title <span>*</span></p>