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

网站搜索功能异常:URL含参数但search.php无结果显示

问题诊断与修复方案

嘿,我一眼就发现了问题的根源——表单提交方式和后端的检测逻辑不匹配,导致你的搜索代码根本没机会执行,所以页面才显示空框!

具体原因

  • 在articles.php里,你的<form>标签没指定method属性,浏览器默认会用GET方法提交请求,所以搜索参数会出现在URL里
  • 但在search.php里,你用isset($_POST['submit-search'])来判断是否触发了搜索,这只会检查POST请求里的参数,自然永远不会进入搜索逻辑,页面就只会显示空的容器

两种修复方案任你选

方案1:把表单改成POST提交(更推荐,符合搜索场景的常规做法)

直接在articles.php的form标签里加上method="POST"就行:

<form action="search.php" method="POST">
  <input type="text" name="search" placeholder="Search">
  <button type="submit" name="submit-search">Get answers</button>
</form>

这样提交后,search.php里的$_POST['submit-search']就能正确检测到,搜索逻辑会正常跑起来。

方案2:让search.php适配GET请求(如果想保留URL里的搜索参数方便分享)

如果你希望别人能直接复制带搜索关键词的URL分享,可以修改search.php的判断逻辑,改用$_GET来获取参数:

<?php include 'connect.php'; ?>
<link rel="stylesheet" type="text/css" href="css/article.css">
<h1>Search Page</h1>
<div class="article-container">
<?php 
// 改成检测GET里的search参数,同时判断非空
if (isset($_GET['search']) && !empty(trim($_GET['search']))){ 
    $search = mysqli_real_escape_string($conn, $_GET['search']);
    $sql = "SELECT * FROM article WHERE a_title LIKE '%$search%' OR a_text LIKE '%$search%' OR a_author LIKE '%$search%' OR a_date LIKE '%$search%'";
    $result = mysqli_query($conn, $sql);
    $queryResult = mysqli_num_rows($result);
    echo "There are ".$queryResult." results!";

    if($queryResult > 0){
        while ($row = mysqli_fetch_assoc($result)){
            echo "<div class='article-box'>
            <h3>".$row['a_title']."</h3>
            <p>".$row['a_text']."</p>
            <p>".$row['a_date']."</p> <!-- 这里你之前写的是<hp>,应该是<p>,顺手帮你修正了这个小bug -->
            <p>".$row['a_author']."</p>
            </div>";
        }
    }else{
        echo "There are no results matching your search! Contact our support so we can add this or if you have a result, add it using the GIVE ANSWER button!";
    }
} 
?>
</div>

顺便提一句:你代码里有个小笔误<hp>,应该是<p>,这个也会导致页面布局错乱,上面的代码已经帮你修正了。

额外建议:升级代码安全性,防止SQL注入

虽然你用了mysqli_real_escape_string,但更稳妥的方式是使用预处理语句,能彻底避免SQL注入风险,给你个示例参考:

if (isset($_POST['submit-search'])){ 
    $search = "%".trim($_POST['search'])."%";
    // 初始化预处理语句
    $stmt = $conn->prepare("SELECT * FROM article WHERE a_title LIKE ? OR a_text LIKE ? OR a_author LIKE ? OR a_date LIKE ?");
    // 绑定参数,四个?s对应四个字符串类型的参数
    $stmt->bind_param("ssss", $search, $search, $search, $search);
    $stmt->execute();
    // 获取结果集
    $result = $stmt->get_result();
    $queryResult = $result->num_rows;
    
    echo "There are ".$queryResult." results!";
    if($queryResult > 0){
        while ($row = $result->fetch_assoc()){
            echo "<div class='article-box'>
            <h3>".$row['a_title']."</h3>
            <p>".$row['a_text']."</p>
            <p>".$row['a_date']."</p>
            <p>".$row['a_author']."</p>
            </div>";
        }
    }else{
        echo "There are no results matching your search! Contact our support so we can add this or if you have a result, add it using the GIVE ANSWER button!";
    }
    // 关闭语句
    $stmt->close();
}

内容的提问来源于stack exchange,提问作者r.kuypers

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:35:07