简体   繁体   中英

Why can't I display the search result?

This is my php code:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'moviefone';
$con    = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $con); // Check connection    if (mysqli_connect_errno()) {  echo "Failed to connect to MySQL: " . mysqli_connect_error();    }    $output = '';
if (isset($_POST["search-text"])) {
    $searchq = $post["search-text"];
    $searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
    $query = mysql_query("SELECT * FROM movies WHERE title LIKE '%$searchq'") or die("could not search");
    $count = mysql_num_rows($query);
    if ($count == 0) {
        $output = "no result found";
    } else {
        while ($row = mysql_fetch_array($query)) {
            $title  = $row['title'];
            $images = $row['images'];
            $output .= '<div>' . $images . ' ' . $title . '</div>';
        }
    }
}
?>

and this my HTML form:

<form id="frm-search" action="search.php" method="post">
    <div class="m-search">
        <input type="hidden" id="search-text-value" name="search-text-value">
        <input type="text" id="search-text" maxlength="150" class="search-text" name="search-tex" placeholder="search" value="" autocomplete="off" onblur="" style="color: rgb(140, 140, 140);">
        <input type="image" src="spray/search.png" value="" class="search-btn">    
        <div class="clear">    </div>
    </div>
</form>

I can not display any result and there are no errors showing on the page. I can not find out what am I doing wrong.

Multiple typos:

if (isset($_POST["search-text"])) {
    $searchq = $post["search-text"];
               ^^^^^---should be $_POST
                      ^^^^^^^^^^---wrong field name, see below

And then in your HTML:

[..snip...] class="search-text" name="search-tex"  [...snip...]
                                      ^^^^^^^^^^---must match the above code

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