由于没有学过PHP,所以只有写伪代码了:
不知道 isdelete=0 是什么作用,暂时没加
sql语句初始为 select * from thread where ''=''if(title不为空){
sql语句加上 ' and title='+title的值
}
if(content不为空){
sql语句加上 ' and content='+content的值
}
if(author不为空){
sql语句加上 ' and author='+author的值
}执行sql
不知道 isdelete=0 是什么作用,暂时没加
sql语句初始为 select * from thread where ''=''if(title不为空){
sql语句加上 ' and title='+title的值
}
if(content不为空){
sql语句加上 ' and content='+content的值
}
if(author不为空){
sql语句加上 ' and author='+author的值
}执行sql
$sql = "select * from ";
$tem = "";if($_POST['title']){
$tem.=(empty($tem)?" WHERE":" AND")."title = '".$_POST['title']'"'";
}
if($_POST['content']){
$tem.=(empty($tem)?" WHERE":" AND")."content = '".$_POST['content']'"'";
}
if($_POST['author']){
$tem.=(empty($tem)?" WHERE":" AND")."author = '".$_POST['author']'"'";
}empty($tem)?($sql." WHERE isdelete = 0"):($sql.$tem." and isdelete = 0");
?>
$sql = "select * from thread ";
foreach($_POST as $k=>$v) {
if(! empty($v)) {
$w[] = "$k='$v'";
}
}
if(count($w) < 1) {
die('至少应输入一个条件');
}
$exec="select * from thread where isdelete=0 and " . join(' and ', $w);
既然楼主还要想更简单的方法,,,可以直接这样写:select * from thread where isdelete=0 and title ='".$title."' and content ='".$content."' ... 记得有单引号!
这样,,,如果输入为空的话MYSQL也找不到结果!
将语句留给MYSQL去判断~!