duanmianzhou5353 2013-12-02 07:18
浏览 22
已采纳

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

hi im working on a search program using php. below is my codes. its running but when i just click the search button it still print an output. what i want is if i will click the button search without imputing something on its text it will print the message"$output = 'there was no search result'". how will i do that? thanks in advance...

<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 
  • 写回答

3条回答 默认 最新

  • dongyan3018 2013-12-02 07:21
    关注

    change this

        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
            }
    

    and please stop using mysql_query

    It was deprecated before it was deprecated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看