表单如下:<form enctype="multipart/form-data" action="index.php" method="post" name="form" onsubmit="return fCheck();">
        <input name="mingzi" size="10" maxlength="4" class="BigInput" type="text">
        <input name="kahao" size="10" maxlength="6" class="BigInput" type="text">        <select name="men" class="BigSelect">
         <option value="前门" selected="selected">前门</option>
         <option value="后门">后门</option>
        </select>        开始日期:<input name="shijian1" size="12" maxlength="10" class="BigInput" type="text">
        结束日期:<input name="shijian2" size="12" maxlength="10" class="BigInput" type="text">
        
        <input value="查询" class="BigButton" type="submit" >&nbsp;&nbsp;
        <input value="重填" class="BigButton" onclick="location='index.php'" type="button">&nbsp;&nbsp;</form>用户可以填写全部表单,也可以不填,或者填写某几个。都能查询。这样服务器SQL怎么写?

解决方案 »

  1.   

    简单,如果查索条件不为空,就and连接
      

  2.   

    具体怎么弄?
    是不是要IF 嵌套 IF 判断 ,具体怎么判断,
    用户可能什么都不填,这也是允许的,
      

  3.   

    对,就是if,例如:
    $username = $_POST['username'];
    $password = $_POST['password'];
    $sql = 'select * from test where type = 1';
    if($username)$sql .= ' and username = "'.$username.'"';
    if($password)$sql .= ' and password = "'.$password.'"';就类似这样就行了
      

  4.   

    $arr = "";
    if($_POST['mingzi']) {
       $arr[] = "mingzi='{$_POST['mingzi']}'";
    }
    if($_POST['kahao']) {
       $arr[] = "kahao='{$_POST['kahao']}'";
    }
    if($_POST['men']) {
       $arr[] = "men='{$_POST['men']}'";
    }
    if($_POST['shijian1']) {
       $arr[] = "shijian1>'{$_POST['shijian1']}'";
    }
    if($_POST['shijian2']) {
       $arr[] = "shijian2>'{$_POST['shijian2']}'";
    }$sql  = "select * from table where ".implode(" and ", $arr);
      

  5.   

    $sql="where 1";
    $mingzi && $sql .= " and `mingzi`='$mingzi'";
    $kahao && $sql .= " and `kahao`='$kahao'";
    ......
      

  6.   

    $arr = "";
    if($_POST['mingzi']) {
      $arr[] = "mingzi='{$_POST['mingzi']}'";
    }
    if($_POST['kahao']) {
      $arr[] = "kahao='{$_POST['kahao']}'";
    }
    if($_POST['men']) {
      $arr[] = "men='{$_POST['men']}'";
    }
    if($_POST['shijian1']) {
      $arr[] = "shijian1>'{$_POST['shijian1']}'";
    }
    if($_POST['shijian2']) {
      $arr[] = "shijian2<'{$_POST['shijian2']}'";
    }$sql = "select * from table where ".implode(" and ", $arr);
      

  7.   

    $arr = array();
    if($_POST['mingzi']) {
      $arr[] = "mingzi='{$_POST['mingzi']}'";
    }
    if($_POST['kahao']) {
      $arr[] = "kahao='{$_POST['kahao']}'";
    }
    if($_POST['men']) {
      $arr[] = "men='{$_POST['men']}'";
    }
    if($_POST['shijian1']) {
      $arr[] = "shijian1>'{$_POST['shijian1']}'";
    }
    if($_POST['shijian2']) {
      $arr[] = "shijian2<'{$_POST['shijian2']}'";
    }$sql = "select * from table where ".implode(" and ", $arr);
      

  8.   

    分页的时候,返回的SQL是:
    select * from table where limit 600,300
      

  9.   

    $arr = array("1=1");
    if($_POST['mingzi']) {
      $arr[] = "mingzi='{$_POST['mingzi']}'";
    }
    if($_POST['kahao']) {
      $arr[] = "kahao='{$_POST['kahao']}'";
    }
    if($_POST['men']) {
      $arr[] = "men='{$_POST['men']}'";
    }
    if($_POST['shijian1']) {
      $arr[] = "shijian1>'{$_POST['shijian1']}'";
    }
    if($_POST['shijian2']) {
      $arr[] = "shijian2<'{$_POST['shijian2']}'";
    }$sql = "select * from table where ".implode(" and ", $arr);