Rippchen/php/add_review.php

70 lines
2.4 KiB
PHP

<?php
include 'db_connection.php';
session_start();
if (!isset($_SESSION['username'])) {
header("Location: login.php"); // Redirect if not logged in
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fk_restaurant_id = sanitize_input($_POST['fk_restaurant_id']);
$bewertung_int = sanitize_input($_POST['bewertung_int']);
$bewertung_str = sanitize_input($_POST['bewertung_str']);
$fk_user_id = $_SESSION['user_id']; // Get user ID from session
// Validate the rating (0-10)
if ($bewertung_int < 0 || $bewertung_int > 10) {
$review_error = "Rating must be between 0 and 10.";
} else {
$sql = "INSERT INTO Bewertung (fk_user_id, fk_restaurant_id, bewertung, bewertung_str) VALUES ($fk_user_id, $fk_restaurant_id, $bewertung_int, '$bewertung_str')";
if ($conn->query($sql) === TRUE) {
$review_success = "Review added successfully!";
} else {
$review_error = "Error: " . $sql . "<br>" . $conn->error;
}
}
}
// Fetch restaurants for the dropdown
$restaurants_sql = "SELECT id, name FROM Restaurant";
$restaurants_result = $conn->query($restaurants_sql);
$restaurants = [];
if ($restaurants_result->num_rows > 0) {
while ($row = $restaurants_result->fetch_assoc()) {
$restaurants[$row['id']] = $row['name'];
}
}
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Review</title>
</head>
<body>
<h2>Add New Review</h2>
<?php if (isset($review_error)): ?>
<p style="color:red;"><?php echo $review_error; ?></p>
<?php endif; ?>
<?php if (isset($review_success)): ?>
<p style="color:green;"><?php echo $review_success; ?></p>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Restaurant:
<select name="fk_restaurant_id" required>
<option value="">Select Restaurant</option>
<?php foreach ($restaurants as $id => $name): ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select><br><br>
Rating (0-10): <input type="number" name="bewertung_int" min="0" max="10" required><br><br>
Review Text: <textarea name="bewertung_str"></textarea><br><br>
<input type="submit" value="Add Review">
</form>
<p><a href="index.html">Back to Dashboard</a></p>
</body>
</html>