嘿,我正在尝试使用if-if/else语句查询数据库,我想要一个选项从数据库中的特定列(在本例中为位置)选择所有结果,另一个选项仅根据表单输入选择项目:
<?php
if(isset($_POST['indexSearchSubmit']))
{
if ($_POST['locationList']=='allLocations')
{
$selectedLocations = '*';
}
else
{
$selectedLocations = $_POST['locationList'];
}
foreach($_POST['industryList'] as $selected)
{
$selectedIndustries = $selected;
$result = mysqli_query($con,"SELECT * FROM currentListings WHERE location = '$selectedLocations' AND industry = '$selectedIndustries'");
while($row = mysqli_fetch_array($result))
{
echo $row['industry'];
echo "<br>";
echo $row['location'];
echo "<br>";
}
}
mysqli_close($con);
}
?>
我感到困惑的是selectedlocation=''我不确定该放什么才能返回所有结果。
任何帮助都将不胜感激
您可以进行
if ($_POST['locationList']=='allLocations')
{
$l = 'IS NOT NULL';
}
else
{
$l = " = '". $_POST['locationList'] . "'";
}
和
$result = mysqli_query($con,"SELECT * FROM currentListings WHERE location " . $l . " AND industry = '$selectedIndustries'");
这样,查询字符串将是
"SELECT * FROM currentListings WHERE location IS NOT NULL AND industry = '$selectedIndustries'"
或
"SELECT * FROM currentListings WHERE location = '[xxx]' AND industry = '$selectedIndustries'"
其中[xxx]是所选选项的值。