Searchbox不会返回任何结果

时间:2014-12-05 16:24:22

标签: php mysql phpmyadmin

我正在尝试在我的sql数据库上进行搜索,但它不会返回任何内容。当我使用var_dump时,它给了我“string(0)”“”我的PHP代码似乎没问题,任何人都可以帮我搞清楚吗?

代码:

的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'];
}
?>

和搜索框,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>

1 个答案:

答案 0 :(得分:0)

由于我没有足够的声誉尚未发表评论,我将在此处写下我的建议。

SQL对我来说似乎有点奇怪。 SELECT nome FROM carros WHERE nome = ?。对我来说,你似乎正在选择你唯一知道的东西。

我要尝试的另一件事是使用$ _POST超级全局而不是$ _REQUEST全局,因为后者在启动每个请求时受PHP影响。 Documentation here

PHP本身看起来很好(很高兴看到有人使用预处理语句......很棒!)并且HTML根据HTML validator有效,假设您要将doctype和字符编码声明为UTF-8。

相关问题