-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
114 lines (102 loc) · 3.36 KB
/
Model.php
File metadata and controls
114 lines (102 loc) · 3.36 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
<?php
class Model {
private $conn = null;
private $table = null;
// Constructor
public function __construct($db) {
$this->conn = $db;
$this->table = Database::$table;
}
// Check if the specified key-value pair already exists in the table
public function isKeyValueUnique($key, $value) {
$sql = "SELECT id FROM $this->table WHERE $key = :value LIMIT 1";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':value', $value);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
return false;
}
return true;
} else {
echo json_encode(["Error" => $stmt->error]);
return false;
}
}
// Create a post
public function create($entitycode, $entity, $user_id, $username, $email) {
$sql = "INSERT INTO $this->table (entitycode, entity, user_id, username, email) VALUES (:entitycode, :entity, :user_id, :username, :email)";
$stmt = $this->conn->prepare($sql);
$params = [
':entitycode' => $entitycode,
':entity' => $entity,
':user_id' => $user_id,
':username' => $username,
':email' => $email
];
if ($stmt->execute($params)) {
if ($stmt->rowCount() > 0) { // Number of affected rows
return true;
}
return false;
}
echo json_encode(["Error" => $stmt->error]);
return false;
}
// Read a single post
public function read_single($id) {
$sql = "SELECT * FROM $this->table WHERE id = :id LIMIT 1";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt;
}
// Read all posts
public function read_all() {
$sql = "SELECT * FROM $this->table";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
return $stmt;
}
// Search for posts (by the properties 'username' and 'email')
public function search($search) {
$sql = "SELECT * FROM $this->table WHERE username LIKE :username OR email LIKE :email";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue(':username', '%'.$search.'%');
$stmt->bindValue(':email', '%'.$search.'%');
$stmt->execute();
return $stmt;
}
// Update a post
public function update($id, $entitycode, $entity, $user_id, $username, $email) {
$sql = "UPDATE $this->table SET entitycode = :entitycode, entity = :entity, user_id = :user_id, username = :username, email = :email WHERE id = :id";
$stmt = $this->conn->prepare($sql);
$params = [
':id' => $id,
':entitycode' => $entitycode,
':entity' => $entity,
':user_id' => $user_id,
':username' => $username,
':email' => $email
];
if ($stmt->execute($params)) {
if ($stmt->rowCount() > 0) { // Number of affected rows
return true;
}
return false;
}
echo json_encode(["Error" => $stmt->error]);
return false;
}
// Delete a post
public function delete($id) {
$sql = "DELETE FROM $this->table WHERE id = :id";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':id', $id);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) { // Number of affected rows
return true;
}
}
return false;
}
} // end: class Model