【问题标题】:Why can't I display the search result?为什么我不能显示搜索结果?
【发布时间】:2013-11-29 18:53:35
【问题描述】:

这是我的 php 代码:

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

这是我的 HTML 表单:

<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>

我无法显示任何结果,并且页面上没有显示错误。我不知道我做错了什么。

【问题讨论】:

  • 稍微格式化一下代码会很有帮助。现在很乱。
  • 您的 PHP 日志中是否有任何错误?
  • @justin 没有错误让我更加困惑。
  • 你没有回显任何数据
  • 您说您的页面上没有显示错误,这并不意味着您的 PHP 日志中没有错误。

标签: php html mysql search


【解决方案1】:

多个错别字:

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

然后在你的 HTML 中:

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

【讨论】:

  • 嘿,谢谢,我终于找到了。但是如何显示带有标题的图像。仅显示图像路径和标题。以及未定义的索引错误发布是什么意思?
猜你喜欢
  • 2019-06-04
  • 2019-10-06
  • 2023-01-16
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
相关资源
最近更新 更多