Php找不到任何结果

时间:2016-11-12 19:11:47

标签: php html css mysqli

嘿我正在尝试制作一个搜索栏,每当我输入要搜索的内容时,总是说“没有结果”。我知道,例如Ninja在VareNavn下的数据库中。

这是我的代码:

<?php

    $tjener = "localhost";
    $brukernavn = "root";
    $passord = "";
    $database = "searchengine";
    $connection = mysqli_connect($tjener,$brukernavn,$passord,$database) or die("could not connect");
    $connection->set_charset("utf8");
    $output = "";
    //collect
    if(isset($_POST["q"])) {
    $searchq = $_POST["q"];

    $query = mysqli_query($connection,"SELECT * FROM products WHERE vareNavn LIKE '%$searchq%' OR desc LIKE '%$searchq%'");
    $count = mysqli_num_rows($query);
    if($count == 0) {
        $output = "There was no search results.";
    } else {
        while ($row = mysqli_fetch_array($query)) {
            $id = $row["vareNr"];
            $title = $row["vareNavn"];
            $desc = $row["desc"];
            $output .= "<h1>" . $title . "</h1><p> " . $desc . "</p><br/>";
        }
    }
}
?>


<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<script>

    function active(){
        var Search = document.getElementById("search");
        if(search.value == "Search..."){
            search.value = "";
            search.placeholder = "Search...";
        }
    }
    function inactive(){
        var Search = document.getElementById("search");
        if(search.value == ""){
            search.value = "Search...";
            search.placeholder = "";
        }
    }

    </script>
</head>
<body>

    <div id="header">
        <div id ="searchBar"> 
            <form action="main.php" method= "post">
                <input type="text" id="search" name="q" placeholder="" value="Search..." autocomplete="off" onMouseDown="active();" onBlur="inactive();"/><input type="submit" id="searchButton" value="Search"/>
            </form>
                 <?php /*
                $query = mysqli_query($connection,"SELECT * FROM products");
                $num_rows = mysqli_num_rows($query);
                while($row = mysqli_fetch_array($query)){
                    $id = $row["vareNr"];
                    $title = $row["vareNavn"];
                    $desc = $row["desc"]; 


                    echo "<h1>" . $title . "</h1><p> " . $desc . "</p><br/>"; 

                } */
            ?>  

        </div>
    </div>
    <div id="main">
        <?php 
                print("$output");
            ?>          

    </div>

    <div id="side">

    </div>



</body>
</html>

0 个答案:

没有答案