-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookModel_class
More file actions
136 lines (131 loc) · 4.07 KB
/
BookModel_class
File metadata and controls
136 lines (131 loc) · 4.07 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
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
<?php
require_once "Book_class.php";
require_once "DBHelper_class.php";
class BookModel extends DBHelper
{
/*插入书籍信息*/
public function addBook ($book)
{
$query = "insert into book (book_name,book_price,book_author,book_time,book_pub,book_intro,book_image,book_dir) values (?,?,?,?,?,?,?,?)";
$stmt = $this->mysqli->prepare($query);
$stmt -> bind_param('sdssssss',$book_name,$book_price,$book_author,$book_time,$book_pub,$book_intro,$book_image,$book_dir);
$book_name=$book->getBook_name();
$book_price=$book->getBook_price();
$book_author=$book->getBook_author();
$book_time=$book->getBook_time();
$book_pub=$book->getBook_pub();
$book_intro=$book->getBook_intro();
$book_image=$book->getBook_image();
$book_dir=$book->getBook_dir();
$stmt->execute();
if($stmt->affected_rows!=1)
{
$this->printError("数据插入失败:".$stmt->error);
return FALSE;
}
else
{
return TRUE;
}
}
/*删除书籍信息*/
public function deleteBook($bookID)
{
// delete image
$book = $this->selectSingleBook($bookID);
unlink('../upload/images/'.$book->getBook_image());
//delete db
$query = "delete from book where book_id='".$bookID."'";
if($this->mysqli->query($query))
{
return TRUE;
}
else
{
$this->printError("数据删除失败");
return FALSE;
}
}
/*修改书籍信息*/
public function modifyBook($book)
{
// delete image
$prebook = $this->selectSingleBook($book->getId());
if($prebook->getBook_image() != $book->getBook_image()){
unlink('../upload/images/'.$prebook->getBook_image());
}
$query = "update book set book_name=?,book_price=?,book_author=?,book_time=?,book_pub=?,book_intro=?,book_dir=?,book_image=? where book_id=?";
$stmt = $this->mysqli->prepare($query);
$stmt ->bind_param('sdssssssi',$book_name,$book_price,$book_author,$book_time,$book_pub,$book_intro,$book_dir,$book_image,$book_id);
$book_name=$book->getBook_name();
$book_price=$book->getBook_price();
$book_author=$book->getBook_author();
$book_time=$book->getBook_time();
$book_pub=$book->getBook_pub();
$book_intro=$book->getBook_intro();
$book_dir=$book->getBook_dir();
$book_image = $book->getBook_image();
$book_id=$book->getId();
$stmt->execute();
if($stmt->affected_rows!=1)
{
$this->printError("数据未更改或更新失败");
$stmt->close();
return FALSE;
}
else
{
$stmt->close();
return TRUE;
}
}
/*获取表中的一条记录*/
public function selectSingleBook($bookID)
{
$query = "select * from book where book_id ='".$bookID."'";
if($result = $this->mysqli->query($query)){
if($row=$result->fetch_assoc()){
$book = new Book($row);
$result->close();
return $book;
}
else
{
$result->close();
$this->printError("获取数据失败");
return FALSE;
}
}
else{
$this->printError("数据查询失败");
return FALSE;
}
}
/*全部数据*/
public function selectAllBook()
{
$query = "select * from book order by book_id";
if($result=$this->mysqli->query($query)){
if($result->num_rows)
{
while ($row=$result->fetch_assoc())
{
$allBook []= new Book($row);
}
$result->close();
return $allBook;
}
else
{
$result->close();
return FALSE;
}
}
else
{
$this->printError("数据查询失败");
return FALSE;
}
}
}
?>