Created
September 9, 2021 11:05
-
-
Save zerobugs-oficial/511d7cf3f39619ea8e55349d82e27cee to your computer and use it in GitHub Desktop.
Sistema de Busca Simples usando PHP e MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = "localhost"; | |
$db = "carros"; | |
$user = "root"; | |
$pass = ""; | |
$mysqli = new mysqli($host, $user, $pass, $db); | |
if($mysqli->connect_errno) { | |
die("Falha na conexão com o banco de dados"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('conexao.php'); | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Sistema de Busca</title> | |
</head> | |
<body> | |
<h1>Lista de Veículos</h1> | |
<form action=""> | |
<input name="busca" value="<?php if(isset($_GET['busca'])) echo $_GET['busca']; ?>" placeholder="Digite os termos de pesquisa" type="text"> | |
<button type="submit">Pesquisar</button> | |
</form> | |
<br> | |
<table width="600px" border="1"> | |
<tr> | |
<th>Marca</th> | |
<th>Veículo</th> | |
<th>modelo</th> | |
</tr> | |
<?php | |
if (!isset($_GET['busca'])) { | |
?> | |
<tr> | |
<td colspan="3">Digite algo para pesquisar...</td> | |
</tr> | |
<?php | |
} else { | |
$pesquisa = $mysqli->real_escape_string($_GET['busca']); | |
$sql_code = "SELECT * | |
FROM veiculos | |
WHERE fabricante LIKE '%$pesquisa%' | |
OR modelo LIKE '%$pesquisa%' | |
OR veiculo LIKE '%$pesquisa%'"; | |
$sql_query = $mysqli->query($sql_code) or die("ERRO ao consultar! " . $mysqli->error); | |
if ($sql_query->num_rows == 0) { | |
?> | |
<tr> | |
<td colspan="3">Nenhum resultado encontrado...</td> | |
</tr> | |
<?php | |
} else { | |
while($dados = $sql_query->fetch_assoc()) { | |
?> | |
<tr> | |
<td><?php echo $dados['fabricante']; ?></td> | |
<td><?php echo $dados['veiculo']; ?></td> | |
<td><?php echo $dados['modelo']; ?></td> | |
</tr> | |
<?php | |
} | |
} | |
?> | |
<?php | |
} ?> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lista de Veículos
Pesquisarreal_escape_string($_GET['busca']); $sql_code = "SELECT * FROM veiculos WHERE fabricante LIKE '%$pesquisa%' OR modelo LIKE '%$pesquisa%' OR veiculo LIKE '%$pesquisa%'"; $sql_query = $mysqli->query($sql_code) or die("ERRO ao consultar! " . $mysqli->error);