Commit b479102f authored by Edin Skoko's avatar Edin Skoko
Browse files

added some php

parent 2e498577
Showing with 325 additions and 161 deletions
+325 -161
......@@ -18,7 +18,7 @@
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link fw-semibold" aria-current="page" href="index.html">Home</a>
<a class="nav-link fw-semibold" aria-current="page" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link fw-semibold" href="register.html">Register</a>
......
<?php
session_start();
class DB {
protected $instance;
......
<?php
require_once __DIR__ . '/database.php';
class USER extends DB{
class USER extends DB {
public function __construct() {
parent::__construct();
}
public function createUser($data) {
$role = 2;
$role = 2; // Default role for a new user
$sql = "INSERT INTO users (username, email, password, role)
VALUES (:inputName, :inputEmail, :inputPassword, :role)";
......@@ -21,22 +23,18 @@ class USER extends DB{
$stmt->bindParam(':role', $role);
// Execute the query and return the result
$result = $stmt->execute();
// Check for errors
if (!$result) {
try {
$result = $stmt->execute();
return $result; // Return the execution result
} catch (PDOException $e) {
// Handle the error (e.g., log it, display an error message)
die("Execute failed: " . $stmt->errorInfo());
echo "Error: " . $e->getMessage();
return false;
}
return $result; // Return the execution result
}
// login user
public function loginUser($email, $password) {
$sql = "SELECT * FROM users WHERE email = :email LIMIT 1";
$stmt = $this->instance->prepare($sql);
$stmt->bindParam(':email', $email);
......@@ -54,8 +52,10 @@ class USER extends DB{
}
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
return false;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
return false;
}
}
......@@ -66,9 +66,5 @@ class USER extends DB{
$count = $stmt->fetchColumn();
return $count > 0; // Returns true if email exists, false otherwise
}
}
?>
\ No newline at end of file
?>
<?php
session_start(); // Start session to access session variables like $_SESSION['user_id']
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_comment'])) {
if (!isset($_SESSION['user_id'])) {
echo "User session not found. Please login first.";
exit();
}
$book_id = isset($_POST['book_id']) ? intval($_POST['book_id']) : null;
$content = isset($_POST['content']) ? htmlspecialchars($_POST['content']) : '';
if (!$book_id || empty($content)) {
echo "Invalid input data.";
exit();
}
try {
require_once '../classes/database.php';
$db = new DB();
$instance = $db->getInstance();
$sql = "INSERT INTO comments (user_id, book_id, content, is_approved) VALUES (:user_id, :book_id, :content, 0)";
$stmt = $instance->prepare($sql);
$stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->bindParam(':book_id', $book_id, PDO::PARAM_INT);
$stmt->bindParam(':content', $content, PDO::PARAM_STR);
// Execute the statement
if ($stmt->execute()) {
// Successful insertion, redirect back to the page where the comment was submitted from
header("Location: ../user_dashboard/book_details.php?book_id=$book_id");
exit();
} else {
// Error inserting comment
echo "Error adding comment.";
}
} catch (PDOException $e) {
echo "Database error: " . var_dump($_SESSION['user_id']);
}
} else {
// Handle invalid request if not coming from POST method or without 'add_comment' parameter
echo "Invalid request.";
}
?>
<?php
require_once "./classes/user.php";
session_start();
$user = new USER();
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
var_dump($_POST);
if (isset($_POST['inputEmail'], $_POST['inputPassword'])) {
$email = $_POST['inputEmail'];
$email = filter_var($_POST['inputEmail'], FILTER_SANITIZE_EMAIL);
$password = $_POST['inputPassword'];
$userData = $user->loginUser($email, $password);
if ($userData) {
$_SESSION['user_id'] = $userData['id'];
$_SESSION['username'] = $userData['username'];
$_SESSION['role'] = $userData['role'];
// Redirect based on role
if ($userData['role'] == 'admin') {
header("Location: ../admin_dashboard/admin.php");
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$userData = $user->loginUser($email, $password);
if ($userData) {
$_SESSION['user_id'] = $userData['id'];
$_SESSION['username'] = $userData['username'];
$_SESSION['role'] = $userData['role'];
// Redirect based on role
if ($userData['role'] == 'admin') {
header("Location: ../admin_dashboard/admin.php");
} else {
header("Location: ../user_dashboard/user_dashboard.php");
}
exit();
} else {
header("Location: ../user_dashboard/user.html");
// Authentication failed
header("Location: ../login.html?error=Invalid email or password.");
exit();
}
exit();
} else {
// Authentication failed
echo "Invalid email or password.";
// Invalid email format
header("Location: ../login.html?error=Invalid email format.");
exit();
}
} else {
echo "Missing POST data";
// Missing POST data
header("Location: ../login.html?error=Missing email or password.");
exit();
}
}
}
$_SESSION['user_id'] = $userData['user_id'];
$_SESSION['username'] = $userData['username'];
$_SESSION['role'] = $userData['role'];
?>
<?php
// Initialize the session
session_start();
// Unset all of the session variables
$_SESSION = [];
// If the session was propagated using cookies, force the browser to delete it
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Destroy the session
session_destroy();
// Redirect to the login page or home page
header("Location: ../index.html");
exit;
?>
<?php
require_once "./classes/user.php";
session_start();
$user = new USER();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['inputName'], $_POST['inputEmail'], $_POST['inputPassword'])) {
$username = $_POST['inputName'];
$email = $_POST['inputEmail'];
$password = $_POST['inputPassword'];
if (strlen($password) < 6) {
header("Location: ../register.html?error=Password too short.");
exit;
}
if ($user->emailExists($email)) {
echo "Email address already exists. Please choose a different email.";
header("Location: ../register.html?error=Email already exists.");
exit;
} else {
$data = [
'username' => $username,
......@@ -21,17 +26,14 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
];
if ($user->createUser($data)) {
header("Location: ../user_dashboard/user.html");
header("Location: ../user_dashboard/user_dashboard.php");
exit;
} else {
echo "Failed to create user. Please try again later.";
}
}
} else {
echo "Missing POST data.";
echo "Missing POST data.";
}
}
?>
<?php
// book_details.php
require_once __DIR__ . "../../php/crud/books.php";
$database = new DB();
$instance = $database->getInstance();
$book = new Book();
// Get book_id from URL parameter
if (isset($_GET['book_id'])) {
$book_id = $_GET['book_id'];
try {
// Fetch book details by book_id
$book = $book->getBookById($book_id);
// Example of displaying details
if ($book) {
echo "<h1>Book Details</h1>";
echo "<h2>" . htmlspecialchars($book['book_title']) . "</h2>";
echo "<p>Author: " . htmlspecialchars($book['first_name'] . ' ' . $book['last_name']) . "</p>";
echo "<p>Category: " . htmlspecialchars($book['category_title']) . "</p>";
echo "<p>Published Year: " . htmlspecialchars($book['year_of_publication']) . "</p>";
echo "<p>Pages: " . htmlspecialchars($book['number_of_pages']) . "</p>";
echo "<p>Description: " . htmlspecialchars($book['description']) . "</p>";
} else {
echo "Book not found.";
}
} catch (PDOException $e) {
echo "Error fetching book details: " . $e->getMessage();
}
} else {
echo "Invalid request. Book ID is missing.";
// Fetch book details
$bookModel = $book->getBookById($book_id);
// Fetch approved comments for the book
$sql_book = "SELECT b.book_title, b.year_of_publication, b.number_of_pages, b.image_url, a.first_name, a.last_name, c.title
FROM books b
JOIN authors a ON b.author_id = a.author_id
JOIN categories c ON b.category_id = c.category_id
WHERE b.book_id = :book_id";
$stmt_book = $instance->prepare($sql_book);
$stmt_book->bindParam(':book_id', $book_id, PDO::PARAM_INT);
$stmt_book->execute();
$book = $stmt_book->fetch(PDO::FETCH_ASSOC); // Fetch as associative array
// Fetch approved comments for the book
$sql_comments = "SELECT u.username, c.content, c.created_at
FROM comments c
JOIN users u ON c.user_id = u.user_id
WHERE c.book_id = :book_id AND c.is_approved = 1";
$stmt_comments = $instance->prepare($sql_comments);
$stmt_comments->bindParam(':book_id', $book_id, PDO::PARAM_INT);
$stmt_comments->execute();
$comments = $stmt_comments->fetchAll(PDO::FETCH_ASSOC); // Fetch as associative array
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Details</title>
<!-- Include Bootstrap CSS or other styles if needed -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<style>
.h-400 {
height: 400px;
}
</style>
</head>
<body>
<?php if ($book): ?>
<div class="container mt-5">
<h1><?= htmlspecialchars($book['book_title']) ?></h1>
<p><strong>Author:</strong> <?= htmlspecialchars($book['first_name'] . ' ' . $book['last_name']) ?></p>
<p><strong>Published Year:</strong> <?= htmlspecialchars($book['year_of_publication']) ?></p>
<p><strong>Pages:</strong> <?= htmlspecialchars($book['number_of_pages']) ?></p>
<p><strong>Category:</strong> <?= htmlspecialchars($book['category_title']) ?></p>
<img src="<?= htmlspecialchars($book['image_url']) ?>" alt="Book Cover" style="max-width: 100%; height: auto;">
<h3>Comments</h3>
<?php if ($comments): ?>
<?php foreach ($comments as $comment): ?>
<div class="comment mb-3">
<p><strong><?= htmlspecialchars($comment['username']) ?>:</strong> <?= htmlspecialchars($comment['comment']) ?></p>
<p><small><?= htmlspecialchars($comment['created_at']) ?></small></p>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>No comments available.</p>
<?php endif; ?>
<!-- NavBar -->
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link fw-semibold" aria-current="page" href="user_dashboard.php">Home</a>
</li>
</ul>
</div>
</div>
<?php else: ?>
<p>Book not found.</p>
</nav>
<!-- NavBar -->
<div class="div d-flex flex-row my-5 col-10 mx-auto ">
<?php if ($book) : ?>
<img src="<?= htmlspecialchars($book['image_url']) ?>" alt="Book Cover" class="h-400">
<div class="div">
<h1><?= htmlspecialchars(ucfirst(strtolower($book['book_title']))) ?></h1>
<p>Author: <?= htmlspecialchars($book['first_name'] . ' ' . $book['last_name']) ?></p>
<p>Category: <?= htmlspecialchars($book['title']) ?></p>
<p>Published Year: <?= htmlspecialchars($book['year_of_publication']) ?></p>
<p>Pages: <?= htmlspecialchars($book['number_of_pages']) ?></p>
</div>
</div>
<!-- Add Comment Form -->
<h3 class="mt-5">Add a Comment</h3>
<h2>Comments</h2>
<?php if ($comments) : ?>
<?php foreach ($comments as $comment) : ?>
<div class="comment">
<p><strong><?= htmlspecialchars($comment['username']) ?></strong> (<?= htmlspecialchars($comment['created_at']) ?>):</p>
<p><?= htmlspecialchars($comment['content']) ?></p>
</div>
<?php endforeach; ?>
<?php else : ?>
<p>No comments yet.</p>
<?php endif; ?>
<form action="../php/crud/comments.php" method="post"> <!-- Adjust path -->
<input type="hidden" name="book_id" value="<?php echo $_GET['book_id']; ?>"> <!-- Assuming book_id is passed via GET -->
<textarea name="content" rows="4" placeholder="Write your comment here..." required></textarea>
<br>
<button type="submit" name="add_comment">Add Comment</button>
</form>
<?php else : ?>
<p>Book not found.</p>
<?php endif; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
</html>
\ No newline at end of file
<?php
require_once "../php/crud/categories.php"; // Include your functions file
$categoryDAO = new Category(); // Assuming $instance is your PDO instance
$categories = $categoryDAO->getAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="./css/index.css">
</head>
<body>
<!-- NavBar -->
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link fw-semibold" aria-current="page" href="user.html">Home</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- NavBar -->
<!-- Banner -->
<!-- Banner -->
<!-- categories -->
<div class="categories d-flex flex-column flex-lg-row flex-wrap">
<?php if (!empty($categories)): ?>
<?php foreach ($categories as $category): ?>
<div class="category-item bg-info text-white fw-bold d-flex justify-content-between p-3 col-2 col-2 flex-wrap mt-3 mt-2 ms-1">
<label class="fs-5 col-10" for="category-<?= htmlspecialchars($category['category_id']) ?>"><?= htmlspecialchars($category['title']) ?></label>
<input type="checkbox" class="form-check-input m-2 rounded-2" id="category-<?= htmlspecialchars($category['category_id']) ?>">
<i class="fa-solid fa-circle-check fa-2x align-self-center"></i>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>No categories found.</p>
<?php endif; ?>
</div>
<!-- categories -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="./js/index.js"></script>
</body>
</html>
\ No newline at end of file
<?php
require_once "../php/crud/categories.php"; // Include your functions file
require_once "../php/crud/books.php"; // Include your functions file
$categoryDAO = new Category(); // Assuming $instance is your PDO instance
$categories = $categoryDAO->getAll();
$book = new Book(); // Assuming $instance is your PDO instance
$books = $book->showBooks();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="./css/index.css">
</head>
<body>
<!-- NavBar -->
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<h4 class="me-3">Public Library</h4>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse d-flex justify-content-between" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link fw-semibold" aria-current="page" href="user_dashboard.php">Home</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link fw-semibold" aria-current="page" href="../php/logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- NavBar -->
<!-- Banner -->
<!-- Banner -->
<!-- categories -->
<div class="categories d-flex flex-column flex-lg-row flex-wrap">
<?php if (!empty($categories)) : ?>
<?php foreach ($categories as $category) : ?>
<div class="category-item bg-info text-white fw-bold d-flex justify-content-between p-3 col-2 col-2 flex-wrap mt-3 mt-2 ms-1">
<label class="fs-5 col-10" for="category-<?= htmlspecialchars($category['category_id']) ?>"><?= htmlspecialchars($category['title']) ?></label>
<input type="checkbox" class="form-check-input m-2 rounded-2" id="category-<?= htmlspecialchars($category['category_id']) ?>">
<i class="fa-solid fa-circle-check fa-2x align-self-center"></i>
</div>
<?php endforeach; ?>
<?php else : ?>
<p>No categories found.</p>
<?php endif; ?>
</div>
<!-- categories -->
<!-- Books Table -->
<div class="container my-5">
<h1 class="mb-4">Explore Books</h1>
<div class="row row-cols-1 row-cols-md-3 g-4">
<!-- PHP loop to dynamically populate books -->
<?php foreach ($books as $book) : ?>
<div class="col-8 mt-4">
<div class="card col-8">
<img src="<?= htmlspecialchars($book['image_url']) ?>" class="card-img-top h-200" alt="Book Cover">
<div class="card-body h-200">
<h5 class="card-title"><?= htmlspecialchars($book['book_title']) ?></h5>
<p class="card-text fw-semibold">By <?= htmlspecialchars($book['first_name'] . ' ' . $book['last_name']) ?></p>
<p class="card-text fw-semibold">Category: <?= htmlspecialchars($book['title']) ?></p>
<p class="card-text fw-semibold">Published Year: <?= htmlspecialchars($book['year_of_publication']) ?></p>
<p class="card-text fw-semibold">Pages: <?= htmlspecialchars($book['number_of_pages']) ?></p>
<!-- Link to book details page -->
<a href="../user_dashboard/book_details.php?book_id=<?= htmlspecialchars($book['book_id']) ?>" class="btn btn-primary">View Details</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<!-- Book Table -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="./js/index.js"></script>
</body>
</html>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment