Skip to content

Instantly share code, notes, and snippets.

@zerobugs-oficial
Created September 9, 2021 11:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zerobugs-oficial/511d7cf3f39619ea8e55349d82e27cee to your computer and use it in GitHub Desktop.
Save zerobugs-oficial/511d7cf3f39619ea8e55349d82e27cee to your computer and use it in GitHub Desktop.
Sistema de Busca Simples usando PHP e MySQL
<?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");
}
<?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>
@softcia
Copy link

softcia commented Mar 24, 2022

<title>Sistema de Busca</title>

Lista de Veículos

Pesquisar
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>
Marca Veículo modelo
Digite algo para pesquisar...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment