简体   繁体   中英

Searchbox don't return any results

I'm trying to make a search on my sql database, but it doesn't return anything. When i use var_dump it gives me "string(0)"" "My PHP code seems to be alright, can anyone help me figure it out ?

The codes:

functions.php:

function buscarCarros($pConexao, $nome){
    $carro = array();
    $query = "SELECT nome FROM carros WHERE nome = ?";

    if ($stmt = mysqli_prepare($pConexao, $query)) {

        mysqli_stmt_bind_param($stmt, "s", $nome);

        mysqli_stmt_execute($stmt);

        mysqli_stmt_bind_result($stmt, $nomeResultado);

        while (mysqli_stmt_fetch($stmt)) {
            $carro[] = $nomeResultado;
        }

        mysqli_stmt_close($stmt);
    }

    return $carro;
}

result_pesquisa.php:

<?php
    include('header.php');
    include_once('menu.php');
    include_once('conecta.php');
    include_once('functions.php');

$nome = $_REQUEST['pesquisas'];

$carro = buscarCarros($conexao, $nome);
foreach ($carro as $carros) {
    echo $carros['nome'];
}
?>

and the searchbox, the menu.php:

<nav class="twelve columns">
    <ul class="menu">
        <li class="menu-item"><a href="index.php">Home</a></li>
        <li class="menu-item"><a href="comprar.php">Comprar</a></li>
        <li class="menu-item"><a href="">Vender</a></li>
        <li class="menu-item"><a href="">Contato</a></li>
    </ul>
    <form id="searchbox" action="result_pesquisa.php" method="POST">
        <input type="text" class="search-top remove-bottom" name="pesquisas" placeholder="Qual carro você procura?">
        <!-- <a href="javascript:document.getElementById('searchbox').submit();"> -->
        <a href="result_pesquisa.php"><span class="icon-top icon-search"></span></a>
    </form>
</nav>

As I do not have enough reputation yet to comment I will write my suggestion here.

The SQL seems a bit weird to me. SELECT nome FROM carros WHERE nome = ? . To me it seems you are selecting the only thing you already know.

The other thing I would try is to use the $_POST super global instead of the $_REQUEST global as the latter is affected by PHP during startup of each request. Documentation here .

The PHP itself seems good (it is nice to see someone using prepared statements...great!) and the HTML is valid according to the HTML validator assuming you are declaring doctype and character encoding to UTF-8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM