-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_crud_101_movies.php
164 lines (148 loc) · 5.82 KB
/
php_crud_101_movies.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Projeto de aplicação CRUD utilizando PDO - Agenda de Contatos
*
*/
// Verificar se foi enviando dados via POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = (isset($_POST["id"]) && $_POST["id"] != null) ? $_POST["id"] : "";
$title = (isset($_POST["title"]) && $_POST["title"] != null) ? $_POST["title"] : "";
$year = (isset($_POST["year"]) && $_POST["year"] != null) ? $_POST["year"] : "";
} else if (!isset($id)) {
// Se não se não foi setado nenhum valor para variável $id
$id = (isset($_GET["id"]) && $_GET["id"] != null) ? $_GET["id"] : "";
$title = NULL;
$year = NULL;
}
// Cria a conexão com o banco de dados
try {
//$conexao = new PDO("mysql:host=localhost;dbname=movies; charset=utf8", "ijbduser2", "mypassword");
$conexao = new PDO("mysql:host=localhost; dbname=movies;
charset=utf8", "ijdbuser2", "mypassword");
$conexao->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conexao->exec("set names utf8");
} catch (PDOException $erro) {
echo "Erro na conexão:".$erro->getMessage();
}
// Bloco If que Salva os dados no Banco - atua como Create e Update
if (isset($_REQUEST["act"]) && $_REQUEST["act"] == "save" && $title != "") {
try {
if ($id != "") {
$stmt = $conexao->prepare("UPDATE Movies SET title=?, year=? WHERE id = ?");
$stmt->bindParam(3, $id);
} else {
$stmt = $conexao->prepare("INSERT INTO Movies (title, year) VALUES (?, ?)");
}
$stmt->bindParam(1, $title);
$stmt->bindParam(2, $year);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
echo "Dados cadastrados com sucesso!";
$id = null;
$title = null;
$year = null;
} else {
echo "Erro ao tentar efetivar cadastro";
}
} else {
throw new PDOException("Erro: Não foi possível executar a declaração sql");
}
} catch (PDOException $erro) {
echo "Erro: ".$erro->getMessage();
}
}
// Bloco if que recupera as informações no formulário, etapa utilizada pelo Update
if (isset($_REQUEST["act"]) && $_REQUEST["act"] == "upd" && $id != "") {
try {
$stmt = $conexao->prepare("SELECT * FROM Movies WHERE id = ?");
$stmt->bindParam(1, $id, PDO::PARAM_INT);
if ($stmt->execute()) {
$rs = $stmt->fetch(PDO::FETCH_OBJ);
$id = $rs->id;
$title = $rs->title;
$year = $rs->year;
} else {
throw new PDOException("Erro: Não foi possível executar a declaração sql");
}
} catch (PDOException $erro) {
echo "Erro: ".$erro->getMessage();
}
}
// Bloco if utilizado pela etapa Delete
if (isset($_REQUEST["act"]) && $_REQUEST["act"] == "del" && $id != "") {
try {
$stmt = $conexao->prepare("DELETE FROM Movies WHERE id = ?");
$stmt->bindParam(1, $id, PDO::PARAM_INT);
if ($stmt->execute()) {
echo "Registo foi excluído com êxito";
$id = null;
} else {
throw new PDOException("Erro: Não foi possível executar a declaração sql");
}
} catch (PDOException $erro) {
echo "Erro: ".$erro->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Movies</title>
</head>
<body>
<form action="?act=save" method="POST" name="form1" >
<h1>Movies</h1>
<hr>
<input type="hidden" name="id" <?php
// Preenche o id no campo id com um valor "value"
if (isset($id) && $id != null || $id != "") {
echo "value=\"{$id}\"";
}
?> />
Title:
<input type="text" name="title" <?php
// Preenche o title no campo title com um valor "value"
if (isset($title) && $title != null || $title != "") {
echo "value=\"{$title}\"";
}
?> />
Year:
<input type="text" name="year" <?php
// Preenche o year no campo year com um valor "value"
if (isset($year) && $year != null || $year != "") {
echo "value=\"{$year}\"";
}
?> />
<input type="submit" value="salvar" />
<input type="reset" value="Novo" />
<hr>
</form>
<table border="1" width="100%">
<tr>
<th>Movie</th>
<th>Year</th>
</tr>
<?php
// Bloco que realiza o papel do Read - recupera os dados e apresenta na tela
try {
$stmt = $conexao->prepare("SELECT * FROM Movies");
if ($stmt->execute()) {
while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {
echo "<tr>";
echo "<td>".$rs->title."</td><td>".$rs->year."</td><td>"
."</td><td><center><a href=\"?act=upd&id=".$rs->id."\">[Alterar]</a>"
." "
."<a href=\"?act=del&id=".$rs->id."\">[Excluir]</a></center></td>";
echo "</tr>";
}
} else {
echo "Erro: Não foi possível recuperar os dados do banco de dados";
}
} catch (PDOException $erro) {
echo "Erro: ".$erro->getMessage();
}
?>
</table>
</body>
</html>