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

时间:2013-04-06 09:52:49

标签: php sql select

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

如果用户输入的是intial>(dinitial)和性别和名称类别(ncat),我试图从表babynames输出名称。但是,如果用户不想只是名称>进入特定类别,那么他想查看从> dinitial开始的所有名称,而不是我的查询应该如何进行?

3 个答案:

答案 0 :(得分: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']."')");

}

答案 1 :(得分: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");

}

答案 2 :(得分: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");

}