This repository was archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
153 lines (147 loc) · 4.86 KB
/
Database.php
File metadata and controls
153 lines (147 loc) · 4.86 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
class Database
{
private $pdo;
private $table_name;
public function connect($table_address, $table_name)
{
$this->table_name = $table_name;
if (!$this->pdo) {
try {
$this->pdo = new PDO("sqlite:" . __DIR__ . "/" . $table_address);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Exception : ' . $e->getMessage();
}
}
}
public function createTable()
{
$this->pdo->exec("CREATE TABLE IF NOT EXISTS " . $this->table_name . " (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT,
Phone TEXT,
Email TEXT,
Places TEXT,
Total INTEGER,
Time TEXT
)");
}
public function createRecord($data)
{
// data - array with keys name and phone
$data["time"] = time();
$sql = "INSERT INTO " . $this->table_name . " (Name, Phone, Email, Places, Total, Time)
VALUES (:name, :phone, :email, :places, :total, :time)";
$request = $this->pdo->prepare($sql);
if ($request) {
$request->execute($data);
} else {
echo $this->pdo->exec("SELECT * FROM users");
}
return $this->pdo->lastInsertId();
}
public function getRecords($filter_by)
{
$sql = "SELECT * FROM " . $this->table_name . " ORDER BY " . $filter_by;
$request = $this->pdo->prepare($sql);
if ($request) {
$request->execute();
}
return $request;
}
public function showRecords($request)
{
echo "<tr><th>ID</th>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Places</th>
<th>Total</th></<tr>";
while ($row = $request->fetch(\PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['Id'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Places'] . "</td>";
echo "<td>" . $row['Total'] . "</td>";
echo "</tr>";
}
}
public function deleteRecord($id) {
$sql = "DELETE FROM " . $this->table_name . " WHERE Id = " . $id;
$request = $this->pdo->prepare($sql);
if ($request) {
$request->execute();
} else {
echo " ERROR IN DELETE ";
}
}
public function updateRecord($data) {
$sql = "UPDATE " . $this->table_name . "
SET Name=:name,
Phone=:phone,
Email=:email,
Places=:places,
Total=:total
WHERE Id=:id";
$request = $this->pdo->prepare($sql);
if ($request) {
$request->execute($data);
} else {
echo " ERROR IN UPDATE ";
}
}
public function createTableLog() {
$this->pdo->exec("CREATE TABLE IF NOT EXISTS " . $this->table_name . " (
id INTEGER PRIMARY KEY AUTOINCREMENT,
order_id INTEGER,
result TEXT,
admin_time TEXT,
user_time TEXT
)");
}
public function createRecordLog($data) {
$sql = "INSERT INTO " . $this->table_name . " (order_id, result, admin_time, user_time)
VALUES (:order_id, :result, :admin_time, :user_time)";
$request = $this->pdo->prepare($sql);
// echo "PREPARED";
if ($request) {
$request->execute($data);
} else {
echo $this->pdo->exec("SELECT * FROM " . $this->table_name );
}
return $this->pdo->lastInsertId();
}
public function getLogRecords()
{
$sql = "SELECT * FROM " . $this->table_name ;
try {
$request = $this->pdo->prepare($sql);
} catch (PDOException $e) {
echo 'Exception : ' . $e->getMessage();
}
if ($request) {
$request->execute();
}
return $request;
}
public function showLogRecords($request)
{
echo "<tr><th>ID</th>
<th>Order_id</th>
<th>Result</th>
<th>Admin Time</th>
<th>User Time</th></<tr>";
while ($row = $request->fetch(\PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['result'] . "</td>";
echo "<td>" . $row['admin_time'] . "</td>";
echo "<td>" . $row['user_time'] . "</td>";
echo "</tr>";
}
}
}