Table header with Mysql query

Asked

Viewed 71 times

1

doesn’t bring what I want, someone can tell me what I’m missing by looking at the code?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sistema de busca interna com PHP/MySQL</title>
</head>

<body>
<form name="frmBusca" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>?a=buscar" >
    <input type="text" name="palavra" />
    <input type="submit" value="Buscar" />
</form>

<?php
// Conexão com o banco de dados
$conn = @mysqli_connect("localhost", "usuario", "senha") or die("Não foi possível a conexão com o Banco");
// Selecionando banco
$db = @mysqli_select_db("produtos", $conn) or die("Não foi possível selecionar o Banco");

// Recuperamos a ação enviada pelo formulário
$a = $_GET['a'];

// Verificamos se a ação é de busca
if ($a == "buscar") {

    // Pegamos a palavra
    $palavra = trim($_POST['palavra']);

    // Verificamos no banco de dados produtos equivalente a palavra digitada
    $sql = mysqli_query("SELECT * FROM produtos WHERE nome LIKE '%".$palavra."%' ORDER BY nome");

    // Descobrimos o total de registros encontrados
    $numRegistros = mysqli_num_rows($sql);

    // Se houver pelo menos um registro, exibe-o
    if ($numRegistros != 0) {
        // Exibe os produtos e seus respectivos preços
        while ($produto = mysqli_fetch_object($sql)) {
            echo $produto->nome . " (R$ ".$produto->valor.") <br />";
        }
    // Se não houver registros
    } else {
        echo "Nenhum produto foi encontrado com a palavra ".$palavra."";
    }
}
?>
</body>
</html>
  • 2

    Error?

  • 2

    "don’t bring what I want" - OK! And what do you want it to bring? Tip: Improve your question to avoid closing, learn more here.

  • Doesn’t bring the information, it brings part of the code

  • Paste the error, "part of the code", which is showing. Check to see if you saved the file with extension .php

  • in the table, I left only one field, for testing, I wanted it to bring as a result select, let’s assume, the field is called id_test it will select, in this case the result should be 1. it had to give me this result in this field of html

  • I’ll take the mistake and leave it here.

  • I had not made the mistake, because I wanted to see if there was something wrong with the code itself, something senseless, but I will put the error, just a moment.

Show 2 more comments

1 answer

1

There’s a lot wrong with the code, for example:

the first is the order of the parameters, the correct is link and then db_name:

$db = @mysqli_select_db($conn, "produtos") or die("Não foi possível selecionar o Banco");

second, entering this page will give that $a is undefined, because you need to check if it is set before assigning, so:

// Recuperamos a ação enviada pelo formulário
isset($_GET['a'])? $a = $_GET['a'] : $a = '';

third the form method is POST and the PHP code expecting GET this strange then it would need to change, but this way it works too:

<form name="frmBusca" method="GET" action="<?php echo $_SERVER['PHP_SELF'] ?>?a=buscar" >
    <input type="text" name="palavra" />
    <input type="submit" value="Buscar" />
</form>

NOTE: I would have to create another page, because sending it is certainly not a good practice.

room the code mysqli_query, expects a connection per parameter:

$sql = mysqli_query($conn, "SELECT * FROM produtos WHERE nome LIKE '%carro%' ORDER BY nome");

now works, has been tested here:

inserir a descrição da imagem aqui

database:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.