使用isset在PHP中无法使用搜索功能

时间:2018-06-16 21:41:40

标签: php html5 mysqli

我有一个基于if(isset($_GET['Search_Button']))返回网站会员的网页。第一个代码块(假设为)根据搜索名称返回成员,第二个代码根据分页返回成员,第三个是默认查询。

我的php如下:

global $con;
//query for search function
if(isset($_GET['Search_Button'])) {
    $members_search = mysqli_real_escape_string($con, $_GET['member_search_input']); 
    // $members_search = $_GET['member_search_input'];
    $name = explode(" ", $members_search);

    if (count($name) == 3) {
        $ViewQuery = "SELECT * FROM users WHERE (first_name LIKE '$name[0]%' AND last_name LIKE '$name[2]%') ORDER BY last_name asc";
    }
    else if (count($name) == 2) {
        $ViewQuery = "SELECT * FROM users WHERE (first_name LIKE '$name[0]%' AND last_name LIKE '$name[1]%') ORDER BY last_name asc";
    }
    else{
        $ViewQuery = "SELECT * FROM users WHERE (first_name LIKE '$name[0]%' OR last_name LIKE '$name[0]%') ORDER BY last_name asc";
    }
    //Check if results were found
    if (mysqli_num_rows($ViewQuery) == 0) {
        $_SESSION["ErrorMessage"] = "We can't find anyone with that name.";
        Redirect_to("site_members.php");
    }
}
//query for Pagination
elseif(isset($_GET['Page'])) {
    $Page = $_GET["Page"];

        if($Page == 0 || $Page < 1) {
            $ShowPostFrom = 0;
        }else 

        $ShowPostFrom = ($Page * 15) - 15;

        $ViewQuery = "SELECT * FROM users ORDER BY last_name asc LIMIT $ShowPostFrom, 15";

}
//default query
else{

$ViewQuery = "SELECT * FROM users ORDER BY last_name asc LIMIT 15;";
}

默认和分页查询工作正常,但是当我进行搜索时,我总是得到$_SESSION["ErrorMessage"] = "We can't find anyone with that name.";我知道这意味着什么都没有被提取。

以下是搜索的html表单:

<div>
    <form action="site_members.php" method="GET" name="members_search">
        <span style="font-size: 14px;">
            <input type="text" style="width: 240px;" placeholder="Member name..." name="member_search_input">
            <input type="submit" name="Search_Button" value="Search">
        </span>
    </form>
</div>

我实际上没有看到任何返回到URL的内容。我的格式action是否应该更改?

1 个答案:

答案 0 :(得分:0)

您需要运行查询并将结果传递给mysqli_num_rows,而不是将查询作为字符串传递。