You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP+MySQL网页搜索框不显示仅见放大镜,求技术解决方案

搜索框仅显示放大镜图标问题排查与修复

以下是针对你遇到的搜索框不显示、仅见放大镜图标问题的排查方向和修复建议:

  • 排查CSS样式异常
    这是最常见的原因,重点检查.form-search、#search、.btn-search相关的CSS规则:

    • 查看输入框是否被设置display: none、visibility: hidden或opacity: 0
    • 检查输入框的宽度是否被设为0,或者容器的overflow: hidden导致输入框被截断
    • 确认放大镜图标是否是按钮的背景图,且按钮样式覆盖了输入框位置
      测试方法:打开浏览器开发者工具(F12),找到输入框元素,禁用相关CSS规则,观察输入框是否显示。
  • 检查全局样式影响
    你引入的header.php可能包含全局表单样式,比如统一隐藏了文本输入框,或者对表单元素的排版做了强制设置,导致搜索输入框被隐藏。可以临时注释掉header.php的引入,测试搜索框是否正常显示。

  • 临时调整HTML结构测试
    给搜索表单添加内联样式,强制保证输入框的显示和排版,排查是否是样式冲突:

    <form action="search.php" method="get" class="form-search">
        <label for="search">Search for:</label>
        <div style="display: flex; align-items: center; gap: 10px;">
            <input type="text" name="term" value="<?= html_escape($term) ?>" 
                   id="search" placeholder="Enter search term" 
                   style="width: 200px; padding: 6px; border: 1px solid #ccc;"
            />
            <input type="submit" value="Search" class="btn btn-search" 
                   style="padding: 6px 12px; cursor: pointer;"
            />
        </div>
    </form>
    

    如果调整后输入框显示,说明问题出在外部CSS文件中。

  • 验证PHP变量渲染
    确认html_escape函数正常工作,不会导致输入框的value属性异常。可以临时将value="<?= html_escape($term) ?>"改为value="测试内容",如果输入框显示,说明$term或html_escape函数存在问题。


你提供的完整代码:

<?php
declare(strict_types = 1);                                // Use strict types
require 'includes/database-connection.php';               // Create PDO object
require 'includes/functions.php';                         // Include functions

$term  = filter_input(INPUT_GET, 'term');                 // Get search term
$show  = filter_input(INPUT_GET, 'show', FILTER_VALIDATE_INT) ?? 3; // Limit
$from  = filter_input(INPUT_GET, 'from', FILTER_VALIDATE_INT) ?? 0; // Offset
$count = 0;                                               // Set count to 0
$articles = [];                                           // Set articles to empty array

if ($term) {                                              // If search term provided
    $arguments['term1'] = '%' . $term .'%';               // Store search term in array
    $arguments['term2'] = '%' . $term .'%';               // three times as placeholders
    $arguments['term3'] = '%' . $term .'%';               // cannot be repeated in SQL

    $sql = "SELECT COUNT(title) FROM article
             WHERE title   LIKE :term1
                OR summary LIKE :term2
                OR content LIKE :term3
               AND published = 1;";                       // How many articles match term
    $count = pdo($pdo, $sql, $arguments)->fetchColumn();  // Return count

    if ($count > 0) {                                     // If articles match term
        $arguments['show'] = $show;                       // Add to array for pagination
        $arguments['from'] = $from;                       // Add to array for pagination
        $sql = "SELECT a.id, a.title, a.summary, a.category_id, a.member_id, 
                       c.name      AS category,
                       CONCAT(m.forename, ' ', m.surname) AS author,
                       i.file      AS image_file,
                       i.alt       AS image_alt 
                  FROM article     AS a
                  JOIN category    AS c    ON a.category_id = c.id
                  JOIN member      AS m    ON a.member_id   = m.id
                  LEFT JOIN image  AS i    ON a.image_id    = i.id
                 WHERE a.title   LIKE :term1
                    OR a.summary LIKE :term2
                    OR a.content LIKE :term3
                   AND a.published = 1
              ORDER BY a.id DESC
                 LIMIT :show 
                OFFSET :from;";                              // Find matching articles
        $articles = pdo($pdo, $sql, $arguments)->fetchAll(); // Run query and get results
    }
}

if ($count > $show) {                                     // If matches is more than show
    $total_pages  = ceil($count / $show);                 // Calculate total pages
    $current_page = ceil($from / $show) + 1;              // Calculate current page
}

$sql = "SELECT id, name FROM category WHERE navigation = 1;"; // SQL to get categories
$navigation  = pdo($pdo, $sql)->fetchAll();               // Get navigation categories
$section     = '';                                        // Current category
$title       = 'Search results for ' . $term;             // HTML <title> content
$description = $title . ' on Creative Folk';              // Meta description content
?>
<?php include 'includes/header.php'; ?>
  <main class="container" id="content">
    <section class="header">
      <form action="search.php" method="get" class="form-search">
        <label for="search"><span>Search for: </span></label>
        <input type="text" name="term" value="<?= html_escape($term) ?>" 
               id="search" placeholder="Enter search term"  
        /><input type="submit" value="Search" class="btn btn-search" />
      </form>
      <?php if ($term) { ?><p><b>Matches found:</b> <?= $count ?></p><?php } ?>
    </section>

    <section class="grid">
      <?php foreach ($articles as $article) { ?>
      <article class="summary">
        <a href="article.php?id=<?= $article['id'] ?>">
          <img src="uploads/<?= html_escape($article['image_file'] ?? 'blank.png') ?>"
               alt="<?= html_escape($article['image_alt']) ?>">
          <h2><?= html_escape($article['title']) ?></h2>
          <p><?= html_escape($article['summary']) ?></p>
        </a>
        <p class="credit">
          Posted in <a href="category.php?id=<?= $article['category_id'] ?>">
          <?= html_escape($article['category']) ?></a>
          by <a href="member.php?id=<?= $article['member_id'] ?>">
          <?= html_escape($article['author']) ?></a>
        </p>
      </article>
      <?php } ?>
    </section>

    <?php if ($count > $show) { ?>
    <nav class="pagination" role="navigation" aria-label="Pagination Navigation">
      <ul>
      <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
        <li>
          <a href="?term=<?= $term ?>&show=<?= $show ?>&from=<?= (($i - 1) * $show) ?>"
            class="btn <?= ($i == $current_page) ? 'active" aria-current="true' : '' ?>">
            <?= $i ?>
          </a>
        </li>
      <?php } ?>
      </ul>
    </nav>
    <?php } ?>

  </main>
<?php include 'includes/footer.php'; ?>

内容的提问来源于stack exchange,提问作者Neo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:48:27