-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathemployees.php
More file actions
147 lines (120 loc) · 4.6 KB
/
employees.php
File metadata and controls
147 lines (120 loc) · 4.6 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
<?php
class Employee{
// Connection
private $conn;
// Table
private $db_table = "Employee";
// Columns
public $id;
public $name;
public $email;
public $age;
public $designation;
public $created;
// Db connection
public function __construct($db){
$this->conn = $db;
}
// GET ALL
public function getEmployees(){
$sqlQuery = "SELECT id, name, email, age, designation, created FROM " . $this->db_table . "";
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute();
return $stmt;
}
// CREATE
public function createEmployee(){
$sqlQuery = "INSERT INTO
". $this->db_table ."
SET
name = :name,
email = :email,
age = :age,
designation = :designation,
created = :created";
$stmt = $this->conn->prepare($sqlQuery);
// sanitize
$this->name=htmlspecialchars(strip_tags($this->name));
$this->email=htmlspecialchars(strip_tags($this->email));
$this->age=htmlspecialchars(strip_tags($this->age));
$this->designation=htmlspecialchars(strip_tags($this->designation));
$this->created=htmlspecialchars(strip_tags($this->created));
// bind data
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":email", $this->email);
$stmt->bindParam(":age", $this->age);
$stmt->bindParam(":designation", $this->designation);
$stmt->bindParam(":created", $this->created);
if($stmt->execute()){
return true;
}
return false;
}
// UPDATE
public function getSingleEmployee(){
$sqlQuery = "SELECT
id,
name,
email,
age,
designation,
created
FROM
". $this->db_table ."
WHERE
id = ?
LIMIT 0,1";
$stmt = $this->conn->prepare($sqlQuery);
$stmt->bindParam(1, $this->id);
$stmt->execute();
$dataRow = $stmt->fetch(PDO::FETCH_ASSOC);
$this->name = $dataRow['name'];
$this->email = $dataRow['email'];
$this->age = $dataRow['age'];
$this->designation = $dataRow['designation'];
$this->created = $dataRow['created'];
}
// UPDATE
public function updateEmployee(){
$sqlQuery = "UPDATE
". $this->db_table ."
SET
name = :name,
email = :email,
age = :age,
designation = :designation,
created = :created
WHERE
id = :id";
$stmt = $this->conn->prepare($sqlQuery);
$this->name=htmlspecialchars(strip_tags($this->name));
$this->email=htmlspecialchars(strip_tags($this->email));
$this->age=htmlspecialchars(strip_tags($this->age));
$this->designation=htmlspecialchars(strip_tags($this->designation));
$this->created=htmlspecialchars(strip_tags($this->created));
$this->id=htmlspecialchars(strip_tags($this->id));
// bind data
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":email", $this->email);
$stmt->bindParam(":age", $this->age);
$stmt->bindParam(":designation", $this->designation);
$stmt->bindParam(":created", $this->created);
$stmt->bindParam(":id", $this->id);
if($stmt->execute()){
return true;
}
return false;
}
// DELETE
function deleteEmployee(){
$sqlQuery = "DELETE FROM " . $this->db_table . " WHERE id = ?";
$stmt = $this->conn->prepare($sqlQuery);
$this->id=htmlspecialchars(strip_tags($this->id));
$stmt->bindParam(1, $this->id);
if($stmt->execute()){
return true;
}
return false;
}
}
?>