0
if($_POST['sinitial']=="no")
{
$result = mysqli_query($con,"SELECT * FROM babyname WHERE (iname= '".$_POST['dinitial']."' AND gender= '".$_POST['gender']."' AND ncat= '".$_POST['ncat']."') OR (iname= '".$_POST['dinitial']."' AND gender= '".$_POST['gender']."')");
?>

如果用户输入需要初始>(dinitial)以及性别和名称类别(ncat),我正在尝试从表babynames中输出名称。但是,如果用户不希望只有名称>进入特定类别,否则他希望查看从 >dinitial 开始的所有名称,而不是我的查询应该如何进行?

4

3 回答 3

0
if($_POST['sinitial']=="no")
{
 $condition = "iname= '".$_POST['dinitial']."' AND gender= '".$_POST['gender']."'";
if(isset($_POST['ncat']) && !empty($_POST['ncat'])) 
 $condition .= "AND ncat= '".$_POST['ncat']."'";
$result = mysqli_query($con,"SELECT * FROM babyname WHERE $condition");

}
于 2013-04-06T10:32:12.883 回答
0
else
{

$result = mysqli_query($con,"SELECT * FROM babyname WHERE (iname like '".$_POST['dinitial']."%' AND gender= '".$_POST['gender']."' AND ncat= '".$_POST['ncat']."') OR (iname like  '".$_POST['dinitial']."%' AND gender= '".$_POST['gender']."')");

}
于 2013-04-06T09:55:52.617 回答
0

让我们试试这种代码方式..

if($_POST['sinitial']=="no")
{
  $condition = "iname= '".$_POST['dinitial']."' AND gender= '".$_POST['gender']."'";

  if(!empty($_POST['ncat'])) 
     $condition .= "AND ncat= '".$_POST['ncat']."'";

$result = mysqli_query($con,"SELECT * FROM babyname WHERE $condition");

}
于 2013-04-06T10:06:31.763 回答