diff --git a/add_review.php b/add_review.php index 821c84c72a0cc31a8a26daeb2f8f5fd68638bcaf..bce9f75dcb252812a40510426a6852455b3f1f10 100644 --- a/add_review.php +++ b/add_review.php @@ -1,4 +1,4 @@ -<?php +<?php include 'components/connect.php'; @@ -11,8 +11,14 @@ if(isset($_GET['get_id'])){ if(isset($_POST['submit'])){ + // ตรวจสอบว่าผู้ใช้ได้เข้าสู่ระบบหรือไม่ if($user_id != ''){ + // ฟังก์ชันสร้าง ID ที่ไม่ซ้ำ + function create_unique_id(){ + return uniqid(); + } + $id = create_unique_id(); $title = $_POST['title']; $title = filter_var($title, FILTER_SANITIZE_STRING); @@ -21,15 +27,23 @@ if(isset($_POST['submit'])){ $rating = $_POST['rating']; $rating = filter_var($rating, FILTER_SANITIZE_STRING); + // ตรวจสอบว่ารีวิวของผู้ใช้คนนี้ได้ถูกเพิ่มไปแล้วหรือไม่ $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!'; + // ถ้าไม่มีรีวิวเพิ่มเข้าไปในฐานข้อมูล + try { + $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!'; + } catch (Exception $e) { + // ถ้ามีข้อผิดพลาดจะจับข้อผิดพลาดและแสดงข้อความ + $error_msg[] = 'Error: ' . $e->getMessage(); + } } }else{ @@ -82,6 +96,7 @@ if(isset($_POST['submit'])){ </section> <!-- add review section ends --> + <!-- sweetalert cdn link --> <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script> @@ -91,4 +106,4 @@ if(isset($_POST['submit'])){ <?php include 'components/alers.php'; ?> </body> -</html> \ No newline at end of file +</html>