-
Notifications
You must be signed in to change notification settings - Fork 0
/
cadatr
108 lines (62 loc) · 1.56 KB
/
cadatr
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
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>Pesquisa - PHP e MySQL</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
if (isset($_POST['acao'])) {
$nome = addslashes($_POST['nome']);
try {
$pdo = new PDO('mysql:host=localhost;dbname=pesquisa_nome', 'root', '');
} catch(PDOException $ex) {
echo 'ERRO NA CONEXÃO COM O BANCO: ' . $ex->getMessage();
exit();
}
$query = 'SELECT * FROM tbl_nomes WHERE nome = :nome';
$pdo = $pdo->prepare($query);
$pdo->bindValue(':nome', $nome);
$pdo->execute();
$nomes = [];
if ($pdo->rowCount() > 0) {
$nomes = $pdo->fetchAll();
}
}
?>
<h1>Pesquisa por Nome:</h1>
<form method="POST">
<label for="nome">Nome:</label><br>
<input type="text" name="nome" id="nome" placeholder="Digite o nome a ser pesquisado" required><br>
<input type="submit" value="Pesquisar" name="acao">
</form>
<?php if (isset($nomes) && count($nomes) > 0) : ?>
<br><br>
<table border="1">
<thead>
<th>ID</th>
<th>Nome</th>
</thead>
<tbody>
<?php for ($i = 0; $i < count($nomes); $i++) : ?>
<td><?= $nomes[$i]['id'] ?></td>
<td><?= $nomes[$i]['nome'] ?></td>
<?php endfor; ?>
</tbody>
</table>
<?php endif; ?>
<?php if (isset($nomes) && count($nomes) == 0) : ?>
<p><strong>Nenhum registro corresponde à pesquisa!</strong></p>
<?php endif; ?>
</body>
</html>
CREATE DATABASE pesquisa_nome;
USE pesquisa_nome;
CREATE TABLE tbl_nomes (
id int(11) PRIMARY KEY AUTO_INCREMENT,
nome varchar(100) NOT NULL
)
INSERT INTO tbl_nomes (id, nome) VALUES
(1, 'Maria'),
(2, 'Roberto');