php 搜索打印所有项目,即使不输入任何字符

php search printing all items even without inputting any character

这是我的搜索代码,它有效,但是当我打印时甚至没有输入任何字符。它仍然在搜索。最糟糕的是,它会打印数据库中的所有项目,我会怎么做。谢谢

 if(isset($_GET['search'])) {

$search_value= $_GET['searchbox'];



//$sql="SELECT idemp,sn FROM employee 
//WHERE idemp like '%$search_value%' OR
//sn like '%$search_value%'";

$sql = "select * from employee where (id_no like '%$search_value%' OR sn like '%$search_value%')";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
  echo "'$result->num_rows' result/s for '$search_value'";
print "<br><br>";

if(isset($_GET['search'])) 将 return 为真,因为它被设置为空字符串。

if(isset($_GET['search']) && $_GET['search'] )

是您要找的条件

您可能需要将 if 条件更改为

if(isset($_GET['search']) && !empty($_GET['searchbox'])){
// do search 
}

首先检查你是否得到任何字符串, 运行你的查询只有当你有东西的时候。

if(isset($_GET['searchbox']) && $_GET['searchbox'] != ''){
    $search_value= $_GET['searchbox'];
    $sql = "select * from employee where (id_no like '%$search_value%' OR sn like '%$search_value%')";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    echo "'$result->num_rows' result/s for '$search_value'";
    print "<br><br>";
}