Repository navigation
Expand file tree
/
Copy pathedit_book.php
More file actions
66 lines (60 loc) · 2.08 KB
/
Copy pathedit_book.php
File metadata and controls
66 lines (60 loc) · 2.08 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
64
65
66
<?php
// if save change happen
if(!isset($_POST['save_change'])){
echo "Something wrong!";
exit;
}
$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']);
$quantity = trim($_POST['quantity']);
if(isset($_FILES['image']) && $_FILES['image']['name'] != ""){
$image = $_FILES['image']['name'];
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$uploadDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . "bootstrap/img/";
$uploadDirectory .= $image;
move_uploaded_file($_FILES['image']['tmp_name'], $uploadDirectory);
}
require_once("./functions/database_functions.php");
$conn = db_connect();
// if publisher is not in db, create new
$findPub = "SELECT * FROM publisher WHERE publisher_name = '$publisher'";
$findResult = mysqli_query($conn, $findPub);
$row = mysqli_fetch_assoc($findResult);
if($row['publisherid']==null){
// insert into publisher table and return id
$insertPub = "INSERT INTO publisher(publisher_name) VALUES ('$publisher')";
$insertResult = mysqli_query($conn, $insertPub);
$select = "Select * from publisher where publisher_name = '$publisher'";
$findResult = mysqli_query($conn, $findPub);
$row = mysqli_fetch_assoc($findResult);
$publisherid = $row['publisherid'];
}else {
$publisherid = $row['publisherid'];
}
$query = "UPDATE books SET
book_title = '$title',
book_author = '$author',
book_descr = '$descr',
book_price = '$price',
publisherid = '$publisherid',
quantity = '$quantity',
category_id_fk = '$category'";
if(isset($image)){
$query .= ", book_image='$image' WHERE book_isbn = '$isbn'";
} else {
$query .= " WHERE book_isbn = '$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");
}
?>