Php 搜索时的输入验证(空输入时显示“无搜索结果”)

Php 搜索时的输入验证(空输入时显示“无搜索结果”),php,mysql,search,Php,Mysql,Search,嗨,我正在使用php开发一个搜索程序。下面是我的代码。它正在运行,但当我单击搜索按钮时,它仍会打印输出。我想要的是,如果我单击“搜索”按钮而不在其文本中输入内容,它将打印消息“$output=‘没有搜索结果’”。我该怎么做?先谢谢你 <input type="text" name="search" placeholder="Search..."> <input type= 'submit' name= 'btnsearch' value= 'search' id= 'btnse

嗨,我正在使用php开发一个搜索程序。下面是我的代码。它正在运行,但当我单击搜索按钮时,它仍会打印输出。我想要的是,如果我单击“搜索”按钮而不在其文本中输入内容,它将打印消息“$output=‘没有搜索结果’”。我该怎么做?先谢谢你

<input type="text" name="search" placeholder="Search...">
<input type= 'submit' name= 'btnsearch' value= 'search' id= 'btnsearch' onclick=         'this.form.action'/>
<input type = 'submit' name = 'download' value = 'save to excel'/>


<?php
mysql_connect("localhost","root","") or die ("could not connect");
mysql_select_db("copylandia") or die ("could not find db");


$output = '';
if(isset($_POST['btnsearch']))
{
$searchq = $_POST['search'];
/*$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);*/

$query = mysql_query("SELECT * FROM user WHERE initial LIKE '%$searchq%' OR lname             LIKE '%$searchq%'") or die ("could not search");
$count = mysql_num_rows($query);

if($count == FALSE)

{

$output = 'There was no search results!';

}
else
{ 
while($row = mysql_fetch_array($query)){
$id = $row['number'];
$Initials = $row['initial'];
$name = $row['fname'];
$lastname = $row['lname'];
$middle = $row['mname'];
$email = $row['emailadd'];
$uname = $row['username'];
$pass = $row['password'];
$Group = $row['group'];
$Position = $row['position'];
$Level1 = $row['level1'];
$Level2 = $row['level2'];
$Level3 = $row['level3'];
$Level4 = $row['level4'];
$Level5 = $row['level5'];
$Level6 = $row['level6'];
$Level7 = $row['level7'];

$output .= 'Initial : '.$Initials.'<br> 
First Name : '.$name.'<br> 
Last Name : '.$lastname.' <br> 
Middle Name : '.$middle.' <br> 
Email Add : '.$email.'<br> 
Username : '.$uname.'<br> 
Password : '.$pass.'<br> 
Group : '.$Group.'<br> 
Position : '.$Position.'<br> 
Level 1 : '.$Level1.'<br> 
Level 2 : '.$Level2.'<br> 
Level 3 : '.$Level3.'<br> 
Level 4 : '.$Level4.'<br> 
Level 5 : '.$Level5.'<br> 
Level 6 : '.$Level6.'<br> 
Level 7 : '.$Level7.'<br>
-------------------------------------<br>';

}

}

print "$output";
}
?>

<?php 

改变这个

    if(isset($_POST['btnsearch']))
    {
    $searchq = $_POST['search'];

    to this




    if(isset($_POST['btnsearch']))
        {
        $searchq =trim(strip_tags( $_POST['search']));  
$query = mysql_query("SELECT * FROM user WHERE initial LIKE '%$searchq%' OR lname    LIKE '%$searchq%'") or die ("could not search");
$count = mysql_num_rows($query);

    // you can also use empty($searchq)
        if(!strlen($searchq) >0 || $count==FALSE)
        {
        print whatever you want to print here
        }
请停止使用mysql\u查询


它在被弃用之前已被弃用。

请检查搜索关键字,而不是下面的查询

$searchq = trim($_POST['search']);
if(!empty($searchq)){
    // query 
}else {
    $output = 'There was no search results!';

}

您可以使用js函数,该函数不允许您在未在搜索框中输入内容的情况下提交


您还可以在php代码中处理提交文本框值为空而不是空的问题。

您应该将建议写为注释bro:)空白处如何?它可以工作,但现在。。当我在文本框中键入错误的值时,没有消息。。我想要的是,如果文本框为空,或者如果我键入了错误的内容。。。我的味精。“没有结果”将出现。。。对不起,我只是新来的。。所以我确实需要你的帮助。。。非常感谢!:)如果(!strlen($searchq)>0 | |$count==FALSE)检查这行…如果仍然有任何问题,请告诉我