-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertBook.php
More file actions
59 lines (49 loc) · 1.6 KB
/
insertBook.php
File metadata and controls
59 lines (49 loc) · 1.6 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
<?php
// Connect to MySQL without specifying the database initially
$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";
$dbCreationQuery = "CREATE DATABASE IF NOT EXISTS $dbname";
if ($conn->query($dbCreationQuery) === TRUE) {
echo "Database '$dbname' created or already exists.<br>";
} else {
die("Error creating database: " . $conn->error);
}
// Select the database
$conn->select_db($dbname);
// Create 'books' table if it doesn't exist
$tableCreationQuery = "CREATE TABLE IF NOT EXISTS books (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
genre VARCHAR(100) NOT NULL,
year INT(4) NOT NULL
)";
if ($conn->query($tableCreationQuery) === TRUE) {
echo "Table 'books' created or already exists.<br>";
} else {
die("Error creating table: " . $conn->error);
}
// Get form data
$title = $_POST['title'];
$author = $_POST['author'];
$genre = $_POST['genre'];
$year = $_POST['year'];
// Insert book data into the database
$sql = "INSERT INTO books (title, author, genre, year) VALUES ('$title', '$author', '$genre', '$year')";
if ($conn->query($sql) === TRUE) {
echo "New book added successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close connection
$conn->close();
?>