-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
63 lines (50 loc) · 1.51 KB
/
test.php
File metadata and controls
63 lines (50 loc) · 1.51 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
<?php
class UserModel{
private $db;
public function __construct($db){
$this->db = $db;
}
public function create($data){
try{
$sql = "INSERT INTO users (name, email, age, status) VALUES ('".$data["name"]."', '".$data["email"]."', ".$data["age"].", '".$data["status"]."')";
return $this->db->exec($sql);
}catch(PDOExeption $e){
echo "An error occurred: " . $e->getMessage();
}
}
public function show(){
try{
$sql = "SELECT * FROM users";;
$result = $this->db->query($sql);
return $result->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
echo "An error occurred: " . $e->getMessage();
}
}
public function showOne($id){
try{
$sql = "SELECT * FROM users WHERE id=".$id.";";
$result = $this->db->query($sql);
return $result->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
echo "An error occurred: " . $e->getMessage();
}
}
public function delete($id){
try{
$sql = "DELETE FROM users WHERE id=".$id.";";
return $this->db->exec($sql);
}catch(PDOException $e){
echo "An error occurred: " . $e->getMessage();
}
}
}
$db = require_once "/config/database.php";
$user = new UserModel($db);
$faked = ['name' => "Mohit",
'age' => 22,
'email' => "mkohli52@gmail.com",
'status' => "active"
];
print_r($user->create($faked));
?>