-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.php
105 lines (42 loc) · 1.08 KB
/
controller.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
<?php require 'db.php'; ?>
<?php
class Post{
//CREATE POST
public function insertPost($title,$content){
global $pdo;
$stmt = $pdo->prepare('INSERT INTO user_post (title,content) VALUES (?,?)');
$stmt->execute([
$title,
$content
]);
return $pdo->lastInsertId();
}
//READ POST
public function readAllPost($fetchType = PDO::FETCH_OBJ){
global $pdo;
$stmt = $pdo->prepare('SELECT * FROM user_post ORDER BY id DESC');
$stmt->execute();
return $stmt->fetchAll($fetchType);
}
public function readPostById($id, $fetchType = PDO::FETCH_OBJ){
global $pdo;
$stmt = $pdo->prepare('SELECT * FROM user_post WHERE id=?');
$stmt->execute([$id]);
return $stmt->fetch($fetchType);
}
//UPDATE POST
public function updatePost($id,$title,$content){
global $pdo;
$stmt = $pdo->prepare('UPDATE user_post SET title=?, content=? WHERE id=?');
$stmt->execute([
$title,
$content,
$id
]);
}
public function deletePost($id){
global $pdo;
$stmt = $pdo->prepare("DELETE FROM user_post WHERE id=?");
$stmt->execute([$id]);
}
}