嘿所有我试图使用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);
}
&GT;
我很困惑的是selectlocation =''我不确定要放入什么,以便它返回所有结果。
任何帮助都会非常感激
答案 0 :(得分:-1)
你可以做到
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'"
OR
"SELECT * FROM currentListings WHERE location = '[xxx]' AND industry = '$selectedIndustries'"
其中[xxx]是所选选项的值。