-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess_AddBook.php
98 lines (88 loc) · 3.9 KB
/
Process_AddBook.php
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
include 'DBconnect.php';
// Get form data
$title = mysqli_real_escape_string($connect, $_POST['title']);
$categories = explode(',', mysqli_real_escape_string($connect, $_POST['categories']));
$year = mysqli_real_escape_string($connect, $_POST['year']);
$authors = explode(',', mysqli_real_escape_string($connect, $_POST['authors']));
$accounts = explode(',', mysqli_real_escape_string($connect, $_POST['accounts']));
$copies = mysqli_real_escape_string($connect, $_POST['copies']);
$cd_copies = mysqli_real_escape_string($connect, $_POST['cd_copies']);
// Check if book already exists
$query = "SELECT book_id FROM book WHERE title='$title' AND year='$year'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) == 0) {
// Book doesn't exist, insert it into book table
$query = "INSERT INTO book (title, year, no_copies, no_cd_copy) VALUES ('$title', '$year', '$copies', '$cd_copies')";
mysqli_query($connect, $query);
$book_id = mysqli_insert_id($connect);
// Insert categories into category table and book_category table
foreach ($categories as $category) {
$category = trim($category);
if ($category != '') {
// Check if category already exists
$query = "SELECT category_id FROM category WHERE category_name='$category'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
// Category already exists, get its ID
$row = mysqli_fetch_assoc($result);
$category_id = $row['category_id'];
} else {
// Category doesn't exist, insert it into category table
$query = "INSERT INTO category (category_name) VALUES ('$category')";
mysqli_query($connect, $query);
$category_id = mysqli_insert_id($connect);
}
// Insert category into book_category table
$query = "INSERT INTO book_category (book_id, category_id) VALUES ('$book_id', '$category_id')";
mysqli_query($connect, $query);
}
}
// Insert authors into author table and book_authors table
foreach ($authors as $author) {
$author = trim($author);
if ($author != '') {
// Check if author already exists
$query = "SELECT author_id FROM author WHERE name='$author'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
// Author already exists, get its ID
$row = mysqli_fetch_assoc($result);
$author_id = $row['author_id'];
} else {
// Author doesn't exist, insert it into author table
$query = "INSERT INTO author (name) VALUES ('$author')";
mysqli_query($connect, $query);
$author_id = mysqli_insert_id($connect);
}
// Insert author into book_authors table
$query = "INSERT INTO book_authors (book_id, author_id) VALUES ('$book_id', '$author_id')";
mysqli_query($connect, $query);
}
}
// Insert accounts into accounts table and book_accounts table
foreach ($accounts as $account) {
$account = trim($account);
if ($account != '') {
// Check if account already exists
$query = "SELECT account_id FROM accounts WHERE account_no='$account'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) > 0) {
// Account already exists, get its ID
$row = mysqli_fetch_assoc($result);
$account_id = $row['account_id'];
} else {
// Account doesn't exist, insert it into accounts table
$query = "INSERT INTO accounts (account_no) VALUES ('$account')";
mysqli_query($connect, $query);
$account_id = mysqli_insert_id($connect);
}
// Insert account into book_accounts table
$query = "INSERT INTO book_accounts (book_id, account_id) VALUES ('$book_id', '$account_id')";
mysqli_query($connect, $query);
}
}
} else {
echo "exists";
}
?>