如果用户从表中填写所有3个显示选择值,并且填充2个指定字段显示所有值,则表单中有3个字段

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']."')");
?>

I am trying to output names from the table babynames if the user inputs are desired intial >(dinitial) and gender and name category (ncat). But if the user doesnt want only the names >coming into a specific category else he wants to view all the names starting from the >dinitial than how should my query go ?

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']."')");

}

Lets try this way of code..

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");

}
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");

}