请问以下这段搜索的代码那边出了错?
当我txtName没有填写内容,提交表单的时候,
sql语句还是只执行最后一个sql语句,即select * from material order by Mat_id,
而我要的效果是它应该执行第一条语句,即select * from material where Mat_name like '%$name%' and Mat_category in ($category)<form action="?action=search" method="post" name="form1" id="form1">
<p>物料名称:
  <input name="txtName" type="text" id="txtName" value="" />
  &nbsp;类别:  
  <select name="sltCategory" id="sltCategory" onchange="document.getElementById('txtCategory').value=document.getElementById('sltCategory').value">
   <option value="">无</option>
    <option value="0">耗材</option>
    <option value="1">电脑</option>
    <option value="2">其他设备</option>
  </select><input name="txtCategory" type="text" id="txtCategory" size="10" value="0,1,2"   />
&nbsp;&nbsp;&nbsp;<input name="btnSearch" type="submit" id="btnSearch" value=" 查询 " />
</form>
<?php 
$class = 'action=search&txtName='.$_REQUEST['txtName'].'&txtCategory='.$_REQUEST['txtCategory'];
echo $class."<br>";
if($_REQUEST['txtName']&&$_REQUEST['action']=="search"){ 
$name = $_REQUEST['txtName']; 
$category = $_REQUEST['txtCategory'];
if($name == null){
$sql = "select * from material where Mat_category in ($category) order by Mat_id";
}else
$sql= "select * from material where Mat_name like '%$name%' and Mat_category in ($category)";
}else{
$sql = "select * from material order by Mat_id";
}
echo $sql;

$page = page($db,$sql,5,$class);
while($row=mysql_fetch_array($page[0])){
?>

解决方案 »

  1.   

    照搬你的代码  没有发现问题
    select * from material where Mat_name like '%1212%' and Mat_category in (0,1,2)
      

  2.   

    是在txtName文本框没有输入内容的时候,不能执行第一条sql语句
      

  3.   

    哦 看错
     if($_REQUEST['txtName']&&$_REQUEST['action']=="search"){ 
            $name = $_REQUEST['txtName']; 
            $category = $_REQUEST['txtCategory'];        
            if($name == null){
                $sql = "select * from material where Mat_category in ($category) order by Mat_id";
            }else
                $sql= "select * from material where Mat_name like '%$name%' and Mat_category in ($category)";
        }
     if($_REQUEST['txtName']&&$_REQUEST['action']=="search"){   你这里判断了啊 $_REQUEST['txtName']有值时  才有机会执行下面的代码
      

  4.   

    if($_REQUEST['txtName']&&$_REQUEST['action']=="search"){
    $_REQUEST['txtName'] 为空,即txtName文本框没有输入内容时,不会进入该分支
      

  5.   


    if($_REQUEST['action']=="search")
    {
    if($_REQUEST['txtName'])
    {
    $sql= "select * from material where Mat_name like '%$name%' and Mat_category in ($category)";
    }
    else
    {
    $sql = "select * from material where Mat_category in ($category) order by Mat_id";
    }
    }
    else
    {
      $sql = "select * from material order by Mat_id"; 
    }
    按照我的理解  你是不是要这样子的效果
      

  6.   

    <?php
        $class = 'action=search&txtName='.$_REQUEST['txtName'].'&txtCategory='.$_REQUEST['txtCategory'];
        echo $class."<br>";
        if($_REQUEST['action']=="search"){
            $name = $_REQUEST['txtName'];
            $category = $_REQUEST['txtCategory'];
            if($name == null){
                $sql = "select * from material where Mat_category in ($category) order by Mat_id";
            }else
                $sql= "select * from material where Mat_name like '%$name%' and Mat_category in ($category)";
        }else{
                $sql = "select * from material order by Mat_id";
        }
        echo $sql;?>