-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_book.php
86 lines (83 loc) · 2.87 KB
/
edit_book.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
<!-- This code to do update on the book information
by update function in sql -->
<?php
// if save change was send in this page
if(!isset($_POST['save_change'])){
echo "Something wrong!";
exit;
}
// entring all the information we have from the book database into variable
$isbn = trim($_POST['isbn']);
$title = trim($_POST['title']);
$author = trim($_POST['author']);
$descr = trim($_POST['descr']);
$price = floatval(trim($_POST['price']));
$publisher = trim($_POST['publisher']);
$category = trim($_POST['category']);
// updating the image
if(isset($_FILES['img']) && $_FILES['img']['name'] != ""){
$image = $_FILES['img']['name'];
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$uploadDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . "images/books/";
$uploadDirectory .= $image;
move_uploaded_file($_FILES['img']['tmp_name'], $uploadDirectory);
}
// asking help from the function
require_once("./include/dBFunctions.inc.php");
$conn = db_connect();// function to connect the sql data
// find publisher and return pubid
// if publisher is not in db, create new
$findPub = "SELECT * FROM publishers WHERE publisher_name = '$publisher'";
$findResult = mysqli_query($conn, $findPub);
if(mysqli_num_rows($findResult)==0){
// insert into publisher table and return id
$insertPub = "INSERT INTO publishers(publisher_name) VALUES ('$publisher')";
$insertResult = mysqli_query($conn, $insertPub);
if(!$insertResult){
echo "Can't add new publisher " . mysqli_error($conn);
exit;
}
$publisherid = mysqli_insert_id($conn);
} else {
$row = mysqli_fetch_assoc($findResult);
$publisherid = $row['publisher_id'];
}
// find category and return catid
// if category is not in db, create new
$findCat = "SELECT * FROM categories WHERE cate_name = '$category'";
$findResult = mysqli_query($conn, $findCat);
if(mysqli_num_rows($findResult)==0){
// insert into category table and return id
$insertCat = "INSERT INTO categories(cate_name) VALUES ('$category')";
$insertResult = mysqli_query($conn, $insertCat);
if(!$insertResult){
echo "Can't add new category " . mysqli_error($conn);
exit;
}
$categoryid = mysqli_insert_id($conn);
} else {
$row = mysqli_fetch_assoc($findResult);
$categoryid = $row['cate_id'];
}
// updating function
$query = "UPDATE books SET
Title = '$title',
Author_name = '$author',
descriptions = '$descr',
Price = '$price',
publisher_id = '$publisherid',
Category = '$categoryid'";
if(isset($image)){
$query .= ", Image_path='$image' WHERE PID = '$isbn'";
} else {
$query .= " WHERE PID = '$isbn'";
}
// two cases for fie , if file submit is on => change a lot
$result = mysqli_query($conn, $query);
if(!$result){
echo "Can't update data " . mysqli_error($conn);
exit;
} else {
header("Location: admin_edit.php?bookisbn=$isbn");
}
?>