-
Notifications
You must be signed in to change notification settings - Fork 43
/
cadastro.php
103 lines (81 loc) · 2.3 KB
/
cadastro.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
<?php
header('Access-Control-Allow-Origin: *');
$connect = new PDO("mysql:host=localhost;dbname=id19500520_db", "id19500520_user", "Xitn^Gbvn3V9Kg<)");
$received_data = json_decode(file_get_contents("php://input"));
$data = array();
if ($received_data->action == 'fetchall') {
$query = "
SELECT * FROM fatec_alunos
ORDER BY id DESC
";
$statement = $connect->prepare($query);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$data[] = $row;
}
echo json_encode($data);
}
if ($received_data->action == 'insert') {
$data = array(
':first_name' => $received_data->firstName,
':last_name' => $received_data->lastName
);
$query = "
INSERT INTO fatec_alunos
(first_name, last_name)
VALUES (:first_name, :last_name)
";
$statement = $connect->prepare($query);
$statement->execute($data);
$output = array(
'message' => 'Aluno Adicionado'
);
echo json_encode($output);
}
if ($received_data->action == 'fetchSingle') {
$query = "
SELECT * FROM fatec_alunos
WHERE id = '" . $received_data->id . "'
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach ($result as $row) {
$data['id'] = $row['id'];
$data['first_name'] = $row['first_name'];
$data['last_name'] = $row['last_name'];
}
echo json_encode($data);
}
if ($received_data->action == 'update') {
$data = array(
':first_name' => $received_data->firstName,
':last_name' => $received_data->lastName,
':id' => $received_data->hiddenId
);
$query = "
UPDATE fatec_alunos
SET first_name = :first_name,
last_name = :last_name
WHERE id = :id
";
$statement = $connect->prepare($query);
$statement->execute($data);
$output = array(
'message' => 'Aluno Atualizado'
);
echo json_encode($output);
}
if ($received_data->action == 'delete') {
$query = "
DELETE FROM fatec_alunos
WHERE id = '" . $received_data->id . "'
";
$statement = $connect->prepare($query);
$statement->execute();
$output = array(
'message' => 'Aluno Deletado'
);
echo json_encode($output);
}
?>