0% found this document useful (0 votes)
10 views

WT assignment 2

The document contains PHP code for a Book Exchange Club application, including user login, registration, and a dashboard for managing books. It handles user authentication, allows users to add and delete books, and displays both the user's books and books from other users. The code includes SQL queries for database interactions and HTML for the user interface.

Uploaded by

akshai9080
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

WT assignment 2

The document contains PHP code for a Book Exchange Club application, including user login, registration, and a dashboard for managing books. It handles user authentication, allows users to add and delete books, and displays both the user's books and books from other users. The code includes SQL queries for database interactions and HTML for the user interface.

Uploaded by

akshai9080
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PPLOGIN

<?php
include 'db_config.php';
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {


$email = $_POST['email'];
$password = $_POST['password'];

$stmt = $conn->prepare("SELECT id, password FROM users WHERE email = ?");


$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $hashed_password);

if ($stmt->fetch() && password_verify($password, $hashed_password)) {


$_SESSION['user_id'] = $id;
header("Loca on: dashboard.php");
} else {
echo "Invalid login creden als!";
}

$stmt->close();
}
?>

REGISTER
<?php
include 'db_config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {


$name = $_POST['name'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);

$stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $password);

if ($stmt->execute()) {
echo "Registra on successful! <a href='login.php'>Login here</a>";
} else {
echo "Error: " . $stmt->error;
}

$stmt->close();
}
?>
DASHBOARD
<?php
include 'db_config.php';
session_start();

if (!isset($_SESSION['user_id'])) {
header("Loca on: login.php");
exit();
}

$user_id = $_SESSION['user_id'];

// Handle adding a book


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_book'])) {
$ tle = $_POST[' tle'];
$author = $_POST['author'];
$genre = $_POST['genre'];
$condi on = $_POST['condi on'];
$stmt = $conn->prepare("INSERT INTO books (user_id, tle, author, genre, `condi on`) VALUES (?,
?, ?, ?, ?)");
$stmt->bind_param("issss", $user_id, $ tle, $author, $genre, $condi on);

if ($stmt->execute()) {
echo "Book added successfully!";
} else {
echo "Error: " . $stmt->error;
}

$stmt->close();
}

// Handle dele ng a book


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_book'])) {
$book_id = $_POST['book_id'];

$stmt = $conn->prepare("DELETE FROM books WHERE id = ? AND user_id = ?");


$stmt->bind_param("ii", $book_id, $user_id);

if ($stmt->execute()) {
echo "Book removed successfully!";
} else {
echo "Error: " . $stmt->error;
}

$stmt->close();
}

// Fetch the current user's books


$user_books = $conn->query("SELECT * FROM books WHERE user_id = $user_id");
// Fetch books added by other users
$other_books = $conn->query("SELECT books.*, users.name AS owner_name
FROM books
JOIN users ON books.user_id = users.id
WHERE books.user_id != $user_id");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, ini al-scale=1.0">
< tle>Book Exchange Club</ tle>
<!-- Link to CSS file -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>Welcome to the Book Exchange Club</h2>

<!-- Add Book Form -->


<form method="POST">
<h3>Add a New Book</h3>
<input type="text" name=" tle" placeholder="Book Title" required><br>
<input type="text" name="author" placeholder="Author" required><br>
<input type="text" name="genre" placeholder="Genre" required><br>
<input type="text" name="condi on" placeholder="Condi on (e.g., New, Used)"
required><br>
<bu on type="submit" name="add_book">Add Book</bu on>
</form>

<hr>
<!-- User's Books -->
<h3>Your Added Books</h3>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Genre</th>
<th>Condi on</th>
<th>Ac on</th>
</tr>
<?php while ($row = $user_books->fetch_assoc()): ?>
<tr>
<td><?php echo $row[' tle']; ?></td>
<td><?php echo $row['author']; ?></td>
<td><?php echo $row['genre']; ?></td>
<td><?php echo $row['condi on']; ?></td>
<td>
<form method="POST" style="display:inline;">
<input type="hidden" name="book_id" value="<?php echo $row['id']; ?>">
<bu on type="submit" name="delete_book">Remove</bu on>
</tr>
<?php endwhile; ?>
</table>

<hr>

</tr>
<?php endwhile; ?>
</table>
</div>
</body>
</html>

You might also like