表单如下:<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" >
<input value="重填" class="BigButton" onclick="location='index.php'" type="button"> </form>用户可以填写全部表单,也可以不填,或者填写某几个。都能查询。这样服务器SQL怎么写?
<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" >
<input value="重填" class="BigButton" onclick="location='index.php'" type="button"> </form>用户可以填写全部表单,也可以不填,或者填写某几个。都能查询。这样服务器SQL怎么写?
是不是要IF 嵌套 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.'"';就类似这样就行了
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);
$mingzi && $sql .= " and `mingzi`='$mingzi'";
$kahao && $sql .= " and `kahao`='$kahao'";
......
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);
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);
select * from table where limit 600,300
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);