-
Notifications
You must be signed in to change notification settings - Fork 0
/
employees.php
124 lines (112 loc) · 3.81 KB
/
employees.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class Employee{
// Connection
private $conn;
// Table
private $db_table = "customer_data";
// 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, phone, address 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,
phone = :phone,
address = :address";
$stmt = $this->conn->prepare($sqlQuery);
// sanitize
$this->name=htmlspecialchars(strip_tags($this->name));
$this->email=htmlspecialchars(strip_tags($this->email));
$this->phone=htmlspecialchars(strip_tags($this->phone));
$this->address=htmlspecialchars(strip_tags($this->address));
// bind data
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":email", $this->email);
$stmt->bindParam(":phone", $this->phone);
$stmt->bindParam(":address", $this->address);
if($stmt->execute()){
return true;
}
return false;
}
// READ single
public function getSingleEmployee(){
$sqlQuery = "SELECT
id,
name,
email,
phone,
address
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->phone = $dataRow['phone'];
$this->address = $dataRow['address'];
}
// UPDATE
public function updateEmployee(){
$sqlQuery = "UPDATE
". $this->db_table ."
SET
name = :name,
email = :email,
phone = :phone,
address = :address
WHERE
id = :id";
$stmt = $this->conn->prepare($sqlQuery);
$this->name=htmlspecialchars(strip_tags($this->name));
$this->email=htmlspecialchars(strip_tags($this->email));
$this->phone=htmlspecialchars(strip_tags($this->phone));
$this->address=htmlspecialchars(strip_tags($this->address));
$this->id=htmlspecialchars(strip_tags($this->id));
// bind data
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":email", $this->email);
$stmt->bindParam(":phone", $this->phone);
$stmt->bindParam(":address", $this->address);
$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;
}
}
?>