-
Notifications
You must be signed in to change notification settings - Fork 1
/
book_add.php
231 lines (209 loc) · 8.53 KB
/
book_add.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/****************************
* File Name: book_add.php *
* Author: Ammar S.A.A *
* Output: Form to add Book *
****************************/
require('config.php');
require(WEBSITE_PATH.'./includes/db_connection.php');
require(WEBSITE_PATH.'./includes/session.php');
include(WEBSITE_PATH.'./includes/header.php');
include(WEBSITE_PATH.'./includes/logo.php');
include(WEBSITE_PATH.'./includes/menu.php');
// add new book
if (isset($_POST['book-add']))
{
$book_pic = $_FILES['book_pic']['name'];
$book_pic_tmp = $_FILES['book_pic']['tmp_name'];
$book_online = $_FILES['book_online']['name'];
$book_online_tmp = $_FILES['book_online']['tmp_name'];
$book_name = trim($_POST['book_name']);
$book_name_urdu = trim($_POST['book_name_urdu']);
$cat_id = $_POST['cat_id'];
$author_id = $_POST['author_id'];
$isbn_no = $_POST['isbn_no'];
$price = $_POST['price'];
//Get author name
$author = "SELECT `id`, `author_name`, `author_name_urdu`, `creation_date`, `updation_date` FROM `tblauthors` WHERE id = {$author_id}";
$result = $conn->query($author);
//Fetches out author name
if ($result && $result->num_rows > 0){
$row = $result->fetch_assoc();
$author_name = $row['author_name'];
}
else{
$msg = "<div class='alert alert-danger text-capitalize'>no author!</div>";
}
//Changes the name of uploaded PDF
$book_online = $book_name." by ".$author_name.".pdf";
//Moves uploaded Online Book to a permanent location
move_uploaded_file($book_online_tmp,"./files/book/$book_online");
//Moves uploaded Profile Pictures to a permanent location
move_uploaded_file($book_pic_tmp,"./images/book_pic/$book_pic");
if (!empty($book_name) && !empty($isbn_no) && !empty($cat_id) && !empty($author_id) && !empty($price)){
if (isset($_GET['id']))
{
$sql_update = "UPDATE `tblbooks` SET
`book_pic` = ?,
`book_online` = ?,
`book_name` = ?,
`book_name_urdu` = ?,
`cat_id` = ?,
`author_id` = ?,
`isbn_number` = ?,
`book_price` = ?
WHERE id = ?";
}
else{
if (IfExist(TBLBOOKS, 'book_name', $book_name)) {
$msg = "<div class='alert alert-info'>Duplicate Entry for <strong>".$book_name."</strong> To View Books' List Click/Tap <a href='".WEBSITE_URL."/book_list.php'>HERE</a>.</div>";
}
elseif (IfExist(TBLBOOKS, 'book_name_urdu', $book_name_urdu)) {
$msg = "<div class='alert alert-info urdu'><p><strong>".$book_name_urdu."</strong>کا اندراج پہلے ہی ہو چکا ہے۔ مکمل لسٹ دیکھنے کے لئے <a class='' href='".WEBSITE_URL."/book_list.php'>یہاں</a> کلک/ٹیپ کریں۔ </p></div>";
}
$sql_insert = "INSERT INTO `tblbooks`(`book_pic`, `book_online`,`book_name`,`book_name_urdu`, `cat_id`, `author_id`, `isbn_number`, `book_price`)
VALUES (?,?,?,?,?,?,?)";
}
if(isset($sql_update)){
// prepare and bind
$stmt = $conn->prepare($sql_update);
$stmt->bind_param("ssssiisdi",$book_pic,$book_online,$book_name,$book_name_urdu,$cat_id,$author_id,$isbn_no,$price,$_GET['id']);
$result = $stmt->execute();
if ($result)
{
$msg = "<div class='alert alert-success text-capitalize'>Book updated successfully😀, To View Books' List Click/Tap <a href='".WEBSITE_URL."/book_list.php'>HERE</a></div>";
}
else{
$msg = "<div class='alert alert-danger text-capitalize'>Errors occured!</div>";
}
}else{
// prepare and bind
$stmt = $conn->prepare($sql_insert);
$stmt->bind_param("ssssiisd",$book_pic,$book_online,$book_name,$book_name_urdu,$cat_id,$author_id,$isbn_no,$price);
$result = $stmt->execute();
if ($result)
{
$msg = "<div class='alert alert-success'>Book added successfully😀, To View Books' List Click/Tap <a href='".WEBSITE_URL."/book_list.php'>HERE</a></div>";
}
else{
$msg = "<div class='alert alert-danger'>Errors occurred!!</div>";
}
}
}
else{
$msg ="<div class='alert alert-danger'>Any Field can not be empty.(except Book Picture and Book Name Urdu)</div>";
}
}
$id = '';
$book_pic = '';
$book_online = '';
$book_name = '';
$book_name_urdu = '';
$cat_id = '';
$author_id = '';
$isbn_no = '';
$price = '';
if (isset($_GET['id']))
{
$select = "SELECT * FROM `tblbooks` WHERE id={$_GET['id']}";
$result = $conn->query($select);
if ($result && $result->num_rows > 0){
$row = $result->fetch_assoc();
$id = $row['id'];
$book_pic = $row['book_pic'];
$book_online = $row['book_online'];
$book_name = $row['book_name'];
$book_name_urdu = $row['book_name_urdu'];
$cat_id = $row['cat_id'];
$author_id = $row['author_id'];
$isbn_no = $row['isbn_number'];
$price = $row['book_price'];
};
}
if (isset($msg))
{
echo $msg;
}
?>
<section id="content">
<div class="page-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col">
<h2>Add Book <hr/></h2>
</div>
</div>
<div class="row">
<div class="col col-sm-12 col-md-12 col-xsm-12">
<!--Book Add Form-->
<form name="book-add" method="post" action="#" enctype="multipart/form-data">
<br />
<input type="hidden" name="book-add" value="book-add" />
<input type="hidden" name="id" value="<?php echo $id;?>" />
<span class="f-img glyphicon glyphicon-book display-1"></span>
<br />
<small>Book Info</small>
<p class="labelenglish"><b>Book Picture:</b></p>
<input type="file" accept="img/*" value="<?php echo $book_pic ?>" name="book_pic" class="labelenglish text-uppercase" />
<p class="labelenglish"><small><b>Note:</b><br /> Your <b class="text-uppercase text-right"><?php if(empty($book_pic)){ echo 'Book Picture'; }else{ echo $book_pic; } ?></b> must not be more than <b>11 MB</b>.</small></p>
<p class="labelenglish"><b>Upload Book (PDF Only):</b></p>
<input type="file" accept=".pdf" value="<?php echo $book_online ?>" name="book_online" class="labelenglish text-uppercase" />
<p class="labelenglish"><small><b>Note:</b><br /> Your <b class="text-uppercase text-right"><?php if(empty($book_online)){ echo 'Online Book (PDF Only)'; }else{ echo $book_online; } ?></b> must not be more than <b>11 MB</b>.</small></p>
<p class="labelenglish"><b>Book Name:</b></p>
<input type="text" name="book_name" value="<?php echo $book_name ?>" class="blank" />
<p class="labelurdu"><b>:کتاب کا نام</b></p>
<input type="text" name="book_name_urdu" value="<?php echo $book_name_urdu ?>" class="blank urdu" />
<p class="labelenglish"><b>Category:</b></p>
<select id="category" name="cat_id" class="blank form-control-lg" >
<?php
$categories = GetCategories($conn);
foreach ($categories as $row) {
$is_selected ="";
if($row['id'] == $cat_id)
{
$is_selected ="selected";
}
echo "<option class='blank' {$is_selected} value='{$row['id']}'>{$row['category_name']}</option>";
}
?>
</select>
<p class="labelenglish"><b>Author:</b></p>
<select id="author" name="author_id" class="blank" >
<?php
$authors = GetAuthors($conn);
foreach ($authors as $row) {
$is_selected ="";
if($row['id'] == $author_id)
{
$is_selected ="selected";
}
echo "<option {$is_selected} value='{$row['id']}'>{$row['author_name']}</option>";
}
?>
</select>
<p class="labelenglish"><b>ISBN No.:</b></p>
<input type="text" name="isbn_no" class="blank" value="<?php echo $isbn_no ?>" />
<p class="labelenglish"><b>Price:</b></p>
<input type="number" name="price" class="blank" value="<?php echo $price ?>" />
<div>
<input type="reset" name="reset" value="Reset" class="btn btn-success"/>
<?php
if (isset($_GET['id'])) {
echo '<input type="submit" name="submit" value="Update" class="btn btn-success" />';
}else{
echo '<input type="submit" name="submit" value="Add" class="btn btn-success" />';
}
?>
</div>
<br />
</form>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<?php
include(WEBSITE_PATH.'./includes/footer.php');
?>