-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
105 lines (94 loc) · 4.05 KB
/
database.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
<div class="database">
<?php
// if (isset($_SESSION["email"])){
// } else{
// header('Location: home.php');
// }
class Database{
public $servername;
public $dbuser;
public $dbpassword;
public $dbname;
public $conn;
// function __construct($serverName){
// echo "Welcome to our site<br>";
// }
// function __destruct(){
// echo "Thank You<br>";
// }
function conn(){
$this->servername = "localhost";
$this->dbuser = "root";
$this->dbpassword = "";
$this->dbname = "book_store";
$this->conn = new mysqli($this->servername,$this->dbuser,$this->dbpassword,$this->dbname);
if ($this->conn->connect_error){
die("Connection failed: ".$conn->connect_error);
} else{
// echo "connected successfully<br>";
}
}
//======================================QUERY======================================
public $query;
public $book;
function presentFromQuery(){
$this->query = "SELECT `id`, `book_title`, `book_description`, `img`
FROM `books`";
$result=$this->conn->QUERY($this->query);
echo "<div class='book_show'>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div id='present_query_home'><img src='".$row["img"]."' alt='' width='75' height='100'><br>"." || ".$row["id"]." || ".$row["book_title"]." || ".$row["book_description"]." || "."<br>"."<a href='http://localhost/RouteExam2/book.php?id=".$row["id"]."'> Show ".$row["book_title"]."</a><br>";
if (isset($_SESSION["email"])){
echo "<a href='http://localhost/RouteExam2/edit.php?id=".$row["id"]."'>edit/delete</a><br>";
}
echo "</div>";
}
} else {
echo "0 Books in store";
}
echo "</div>";
}
function presentFromQuerySingle($id){
$this->query = "SELECT `id`, `book_title`, `book_description`, `img`
FROM `books`
WHERE `book_title` ='$id' OR `id` = '$id'";
echo "<div class='book'>";
$result=$this->conn->QUERY($this->query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src='".$row["img"]."' alt='' width='250' height='350'><br>";
echo " ID: ".$row["id"]."<br>"."Book name: ".$row["book_title"]."<br>"."Description: ".$row["book_description"]."<br>";
if (isset($_SESSION["email"])){
echo "<a href='http://localhost/RouteExam2/edit.php?id=".$row["id"]."'>edit/delete</a><br>";
}
}
} else {
echo "0 Books in store";
}
echo "</div>";
}
function insertIntoQuery($book_title, $book_description, $img){
$this->query = "INSERT INTO `books`(`book_title`, `book_description`, `img`)
VALUES ('$book_title','$book_description', '$img')";
$result=$this->conn->QUERY($this->query);
echo "Added!<br>";
$this->presentFromQuerySingle($book_title);
}
function editQuery($id, $book_title, $book_description, $img){
$this->query = "UPDATE `books`
SET `book_title`='$book_title',`book_description`='$book_description',`img`='$img'
WHERE `id` = '$id'";
$result=$this->conn->QUERY($this->query);
echo "Edited!<br>";
$this->presentFromQuerySingle($book_title);
}
function deleteQuery($id){
$this->query = "DELETE FROM `books`
WHERE `id` = '$id'";
$result=$this->conn->QUERY($this->query);
echo "<span id ='delete'>Deleted!<br></span>";
}
}
?>
</div>