在PHP中从数据库中选择字符串

在PHP中从数据库中选择字符串,php,Php,我有一个网页,你可以搜索数据库。用户可以搜索5个不同的字段,页面通过post发送输入。点击搜索时,某些字段可以为空。我可以使用一个很好的select语句,而不是一大堆if语句 $Country = $_POST['Country']; $Gender = $_POST['Gender']; $lastName = $_POST['lastName']; $firstName = $_POST['firstName']; $sport = $_POST['sport']; //select

我有一个网页,你可以搜索数据库。用户可以搜索5个不同的字段,页面通过post发送输入。点击搜索时,某些字段可以为空。我可以使用一个很好的select语句,而不是一大堆if语句

$Country = $_POST['Country'];
$Gender = $_POST['Gender'];
$lastName = $_POST['lastName'];
$firstName = $_POST['firstName'];
$sport = $_POST['sport'];   

//selects sport and country
    if  (($lastName == null) && ($firstName == null) && ($Gender == null))
    {
    $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON          (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (name = '$Country') AND (sport ='$sport') ";
    }


  //selects country and gender and sport
  if (($lastName == null) && ($firstName == null))
    {

        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (name = '$Country') AND (gender ='$Gender') AND (sport = '$sport')";
    }


    //selects country and last and first name
    else if ($Gender == null)
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (name = '$Country') AND (firstName LIKE '%$firstName%') AND (lastName LIKE '%$lastName%') AND (sport = '$sport') ";
    }

    //selects sport, gender, last name and country 
    else if  ($firstName == null)
    {
    $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (name = '$Country') AND (sport ='$sport') AND (gender ='$Gender') AND (lastName LIKE '%$lastName%') ";
    }

    //selects sport, gender, first name and country 
    else if  ($lastName == null)
    {
    $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (name = '$Country') AND (sport ='$sport') AND (gender ='$Gender') AND (firstName LIKE '%$firstName%') ";
    }

    //selects just country
 if (($Gender == null) && ($lastName == null) && ($firstName == null) && ($sport == null))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE  (name ='$Country') ";
    }


         //selects just sport
     else if (($lastName == null) && ($firstName == null) && ($Gender == null) && ($Country == 'country'))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (sport = '$sport') ORDER BY sport ";
    }

         //selects just last name
     else if (($sport == null) && ($firstName == null) && ($Gender == null) && ($Country == 'country'))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (lastName = '$lastName') ORDER BY sport ";
    }

    //selects gender and last name
    else if (($Country == 'country') && ($firstName == null))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (lastName LIKE '%$lastName%') AND (gender LIKE '%$Gender%') AND (sport = '$sport') ";

    }


    //selects gender and first name
    else if (($Country == 'country') && ($lastName == null))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (firstName LIKE '%$firstName%') AND (gender = '$Gender') AND (sport = '$sport')  ";

    }


    //selects country, sport and first name
    else if (($Gender == null) && ($lastName == null))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (firstName LIKE '%$firstName%') AND (sport = '$sport') AND (name = '$Country') ";

    }


    //selects last name, sport and first name
    else if (($Gender == null) && ($Country == 'country'))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (firstName LIKE '%$firstName%') AND (sport = '$sport') AND (lastName LIKE '%$lastName%') ";

    }
    // selects sport and gender
    else if (($Country == null) && ($lastName == null) && ($firstName == null)) 
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (gender = '%Gender%') AND (sport = '$sport') ";
    }

    // selects gender
    else if (($Country == null) && ($lastName == null) && ($firstName == null) && ($sport == null ) ) 
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (gender = '%Gender%')  ";
    }

    // selects country and last name 
    else if (($Gender == null) && ($firstName == null ) && ($sport == null))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE  (name = '$Country') AND (lastName LIKE '%$lastName%') ";
    }


    // selects country and first name 
else  if (($Gender == null) && ($lastName == null ) && ($sport == null))
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE  (name = '$Country') AND (firstName LIKE '%$firstName%') ";
    }


        // selects all
    else if (($Gender == null) && ($firstName == null ) && ($sport == null) && ($lastName == null) && ($Country == 'country') )
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode )";
    }
    // selects if all feilds full
    else 
    {
        $selectString = "SELECT * FROM tblCountry JOIN tblAthletes ON (tblAthletes.countryCode = tblCountry.countryCode ) WHERE (name = '$Country') AND (gender ='$Gender') AND (lastName LIKE '%$lastName%') AND (firstName LIKE '%$firstName%') ORDER BY lastName ";
    }

    $result = mysql_query($selectString);


    while($row = mysql_fetch_assoc($result))
    {
    echo"<tr>";
        foreach($row as $index=>$value)
        {
            if(($index == 'flagImage')||($index == 'atheleteImage'))
            {
                //Gets images
                echo"<td><img title='competitor' alt='' src='images/$value' width='80' height='80'/></td>";
            }
         else
            {
                echo("<td>$value</td>");
            }
        }
    echo"</tr>";
    }
    echo"</table>";
    echo"</div>";

}
$Country=$\u POST['Country'];
$Gender=$_POST['Gender'];
$lastName=$\u POST['lastName'];
$firstName=$_POST['firstName'];
$sport=$_POST['sport'];
//选择运动和国家
如果($lastName==null)&&($firstName==null)&&($Gender==null))
{
$selectString=“SELECT*FROM TBLCONTRY JOIN tblAthletes ON(tblAthletes.countryCode=TBLCONTRY.countryCode),其中(名称='$Country')和(运动='$sport')”;
}
//选择国家、性别和体育
如果($lastName==null)和($firstName==null))
{
$selectString=“SELECT*FROM TBLCONTRY JOIN TBLATTLETES ON(TBLATTLETES.countryCode=TBLCONTRY.countryCode),其中(名称=“$Country”)和(性别=“$gender”)以及(运动=“$sport”);
}
//选择国家、姓氏和名字
else if($Gender==null)
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(name='$Country')和(firstName类似“%$firstName%”)和(lastName类似“%$lastName%”)和(sport='$sport');
}
//选择运动、性别、姓氏和国家
else if($firstName==null)
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(name='$Country')和(sport='$sport')和(gender='$gender')和(lastName类似“%$lastName%”);
}
//选择运动、性别、名字和国家
else if($lastName==null)
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(name='$Country')和(sport='$sport')和(gender='$gender')和(firstName类似“%$firstName%”);
}
//选择公正的国家
如果($SEXT==null)&&($lastName==null)&&($firstName==null)&&($sport==null))
{
$selectString=“SELECT*FROM TBLCONTRY JOIN tblAthletes ON(tblAthletes.countryCode=TBLCONTRY.countryCode),其中(name='$Country')”;
}
//选择正义运动
如果($lastName==null)&&($firstName==null)&&($Gender==null)&&($Country==Country'),则为else
{
$selectString=“SELECT*FROM TBLCONTRY JOIN tblAthletes ON(tblAthletes.countryCode=TBLCONTRY.countryCode),其中(sport='$sport')按运动排序”;
}
//只选择姓氏
如果($sport==null)&&($firstName==null)&&($Gender==null)&&($Country==Country'),则为else
{
$selectString=“SELECT*FROM TBLCONTRY JOIN tblAthletes ON(tblAthletes.countryCode=TBLCONTRY.countryCode),其中(lastName='$lastName')按运动排序”;
}
//选择性别和姓氏
else if(($Country=='Country')&&($firstName==null))
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(lastName类似“%$lastName%”)和(gender类似“%$gender%”)和(sport='$sport');
}
//选择性别和名字
else if(($Country=='Country')&&($lastName==null))
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(firstName,如“%$firstName%”)和(gender=“$gender”)和(sport=“$sport”);
}
//选择国家、运动和名字
else如果($Gender==null)和($lastName==null))
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(firstName类似“%$firstName%”)和(sport=“$sport”)和(name=“$Country”);
}
//选择姓氏、运动和名字
如果($SENDERY==null)和($Country=='Country'),则为else
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(firstName像“%$firstName%”)和(sport像“%$lastName%”)和(lastName像“%$lastName%”);
}
//选择运动和性别
else如果($Country==null)和($lastName==null)和($firstName==null))
{
$selectString=“SELECT*FROM TBLCONTRY JOIN TBLATTLETES ON(TBLATTLETES.countryCode=TBLCONTRY.countryCode),其中(性别='%SENDER%')和(运动='$sport')”;
}
//选择性别
如果($Country==null)&&($lastName==null)&&($firstName==null)&&($sport==null))
{
$selectString=“SELECT*FROM TBLCONTRY JOIN tblAthletes ON(tblAthletes.countryCode=TBLCONTRY.countryCode),其中(性别=“%SEND性别%”)”;
}
//选择国家和姓氏
else如果($Gender==null)和($firstName==null)和($sport==null))
{
$selectString=“SELECT*FROM tblcontry JOIN tblAthletes ON(tblAthletes.countryCode=tblcontry.countryCode),其中(name='$Country')和(lastName类似“%$lastName%””);
}
//选择国家和名字
else如果($Gender==null)和($lastName==null)和($sport==null))
{
$selectString=“SELECT*FROM TBLCONTRY JOIN tblAthletes ON(tblAthletes.countryCode=TBLCONTRY.countryCode),其中(name='$Country')和(firstName类似“%$firstName%””);
}
//选择全部
如果($Gender==null)和($firstName==null)和($sport==null)和($lastName==null)和($Country==Country'),则为else
{
$selectString=“选择*从TBLContry加入tblAthletes ON(tblAthletes.countryCode=TBLContry.countryCode)”;
}
//选择所有字段是否已满
其他的
{
$selectString=“SELECT*FROM TBLCONTRY JOIN tblAthletes ON(tblAthletes.countryCode=TBLCONTRY.countryCode),其中(名称=‘$Country’)和(性别=‘$GENDERY’)
SELECT
    *
FROM
    your_table_here
WHERE
        (('' = :country) OR country = :country)
    AND (('' = :gender) OR gender = :gender)
    AND (('' = :lastName) OR lastName = :lastName)
    AND (('' = :firstName) OR firstName = :firstName)
    AND (('' = :sport) OR sport = :sport)
;
SELECT
    *
FROM
    your_table_here
WHERE
        (IS NULL(:country) OR country = :country)
    AND (IS NULL(:gender) OR gender = :gender)
    AND (IS NULL(:lastName) OR lastName = :lastName)
    AND (IS NULL(:firstName) OR firstName = :firstName)
    AND (IS NULL(:sport) OR sport = :sport)
SELECT `Country`, `Gender`, `lastName`, `firstName`, `sport` FROM TABLE_NAME WHERE Country='$country'...etc