-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreserveBook.php
More file actions
63 lines (53 loc) · 1.81 KB
/
reserveBook.php
File metadata and controls
63 lines (53 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
// Connect to the MySQL server
$servername = "localhost";
$username = "root"; // Update with your database username
$password = ""; // Update with your database password
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database if it doesn't exist
$dbname = "library_db";
$sql = "CREATE DATABASE IF NOT EXISTS $dbname";
if ($conn->query($sql) !== TRUE) {
die("Error creating database: " . $conn->error);
}
// Select the database
$conn->select_db($dbname);
// Create reservations table if it doesn't exist
$table_sql = "CREATE TABLE IF NOT EXISTS reservations (
id INT AUTO_INCREMENT PRIMARY KEY,
book_id INT NOT NULL,
user_name VARCHAR(255) NOT NULL,
user_email VARCHAR(255) NOT NULL,
reservation_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
if ($conn->query($table_sql) !== TRUE) {
die("Error creating table: " . $conn->error);
}
// Get form data
$book_id = $_POST['book_id'];
$user_name = $_POST['user_name'];
$user_email = $_POST['user_email'];
$reservation_date = $_POST['reservation_date'];
// Check if the book is already reserved
$check_sql = "SELECT * FROM reservations WHERE book_id='$book_id'";
$result = $conn->query($check_sql);
if ($result->num_rows > 0) {
echo "Sorry, this book is already reserved.";
} else {
// Insert the reservation into the database
$sql = "INSERT INTO reservations (book_id, user_name, user_email, reservation_date) VALUES ('$book_id', '$user_name', '$user_email', '$reservation_date')";
if ($conn->query($sql) === TRUE) {
echo "Book reserved successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Close connection
$conn->close();
?>