-
Notifications
You must be signed in to change notification settings - Fork 71
/
update.php
191 lines (167 loc) · 7.65 KB
/
update.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
require 'banco.php';
$id = null;
if (!empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if (null == $id) {
header("Location: index.php");
}
if (!empty($_POST)) {
$nomeErro = null;
$enderecoErro = null;
$telefoneErro = null;
$emailErro = null;
$sexoErro = null;
$nome = $_POST['nome'];
$endereco = $_POST['endereco'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];
$sexo = $_POST['sexo'];
//Validação
$validacao = true;
if (empty($nome)) {
$nomeErro = 'Por favor digite o nome!';
$validacao = false;
}
if (empty($email)) {
$emailErro = 'Por favor digite o email!';
$validacao = false;
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErro = 'Por favor digite um email válido!';
$validacao = false;
}
if (empty($endereco)) {
$enderecoErro = 'Por favor digite o endereço!';
$validacao = false;
}
if (empty($telefone)) {
$telefoneErro = 'Por favor digite o telefone!';
$validacao = false;
}
if (empty($sexo)) {
$sexoErro = 'Por favor preenche o campo!';
$validacao = false;
}
// update data
if ($validacao) {
$pdo = Banco::conectar();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE pessoa set nome = ?, endereco = ?, telefone = ?, email = ?, sexo = ? WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($nome, $endereco, $telefone, $email, $sexo, $id));
Banco::desconectar();
header("Location: index.php");
}
} else {
$pdo = Banco::conectar();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM pessoa where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$nome = $data['nome'];
$endereco = $data['endereco'];
$telefone = $data['telefone'];
$email = $data['email'];
$sexo = $data['sexo'];
Banco::desconectar();
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<!-- using new bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Atualizar Contato</title>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="card">
<div class="card-header">
<h3 class="well"> Atualizar Contato </h3>
</div>
<div class="card-body">
<form class="form-horizontal" action="update.php?id=<?php echo $id ?>" method="post">
<div class="control-group <?php echo !empty($nomeErro) ? 'error' : ''; ?>">
<label class="control-label">Nome</label>
<div class="controls">
<input name="nome" class="form-control" size="50" type="text" placeholder="Nome"
value="<?php echo !empty($nome) ? $nome : ''; ?>">
<?php if (!empty($nomeErro)): ?>
<span class="text-danger"><?php echo $nomeErro; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($enderecoErro) ? 'error' : ''; ?>">
<label class="control-label">Endereço</label>
<div class="controls">
<input name="endereco" class="form-control" size="80" type="text" placeholder="Endereço"
value="<?php echo !empty($endereco) ? $endereco : ''; ?>">
<?php if (!empty($enderecoErro)): ?>
<span class="text-danger"><?php echo $enderecoErro; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($telefoneErro) ? 'error' : ''; ?>">
<label class="control-label">Telefone</label>
<div class="controls">
<input name="telefone" class="form-control" size="30" type="text" placeholder="Telefone"
value="<?php echo !empty($telefone) ? $telefone : ''; ?>">
<?php if (!empty($telefoneErro)): ?>
<span class="text-danger"><?php echo $telefoneErro; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($emailErro) ? 'error' : ''; ?>">
<label class="control-label">Email</label>
<div class="controls">
<input name="email" class="form-control" size="40" type="text" placeholder="Email"
value="<?php echo !empty($email) ? $email : ''; ?>">
<?php if (!empty($emailErro)): ?>
<span class="text-danger"><?php echo $emailErro; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($sexoErro) ? 'error' : ''; ?>">
<label class="control-label">Sexo</label>
<div class="controls">
<div class="form-check">
<p class="form-check-label">
<input class="form-check-input" type="radio" name="sexo" id="sexo"
value="M" <?php echo ($sexo == "M") ? "checked" : null; ?>/> Masculino
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="sexo" id="sexo"
value="F" <?php echo ($sexo == "F") ? "checked" : null; ?>/> Feminino
</div>
</p>
<?php if (!empty($sexoErro)): ?>
<span class="text-danger"><?php echo $sexoErro; ?></span>
<?php endif; ?>
</div>
</div>
<br/>
<div class="form-actions">
<button type="submit" class="btn btn-warning">Atualizar</button>
<a href="index.php" type="btn" class="btn btn-default">Voltar</a>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>