我有一个包含3个元素的搜索表单 关键词,行业(下拉)&位置
我正在尝试创建搜索功能,如果选择了所有3个元素或基于单个值或者提交了$ _POST数据的任何元素的组合,将查询数据库
它只是不起作用
我的查询代码在
之下 if(isset($_POST['keywords']))
{
$keywords = $_POST['keywords'];
$viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') LIMIT 0,50");
$resultsfor = $_POST['keywords'];
}
elseif(isset($_POST['keywords']) && isset($_POST['location']))
{
$keywords = $_POST['keywords'];
$location = $_POST['location'];
$viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location` LIKE '%$location%') LIMIT 0,50");
$resultsfor = $keywords .' jobs in '.$location;
}
elseif(isset($_POST['keywords']) && isset($_POST['location']) && !empty($_POST['industry']))
{
$keywords = $_POST['keywords'];
$location = $_POST['location'];
$catno = $_POST['industry'];
$viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location` LIKE '%$location%') AND (`catno` = '$catno') LIMIT 0,50");
$resultsfor = $keywords .' jobs in '.$location;
}
elseif(isset($_POST['industry']) && empty($_POST['location']))
{
$industry = $_POST['industry'];
$viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE `catno` = '$industry' LIMIT 0,50");
$resultsfor = $_POST['industry']. ' jobs';
}
elseif(isset($_POST['industry'])&& isset($_POST['location']))
{
$industry = $_POST['industry'];
$location = $_POST['location'];
$viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`catno` = '$industry') AND (`location` LIKE '%$location%') LIMIT 0,50");
$resultsfor = $_POST['industry']. ' jobs in '.$location;
}
elseif(isset($_POST['location']))
{
$location = $_POST['location'];
$viewq = mysqli_query($con,"SELECT * FROM `listings` WHERE (`location` LIKE '%$location%') LIMIT 0,50");
$resultsfor = $_POST['location']. ' jobs';
}
$view = mysqli_fetch_assoc($viewq);
非常感谢一些帮助,我花了很多时间在这上面 提前致谢
**它确实提取数据虽然不正确,如果我搜索仓库(留下行业下拉空白)但设置位置到莱斯特仍然查询所有结果而不是莱斯特 如果我从下拉菜单中选择行业并将其他字段留空,我将获得所有类别中的所有作业,而不仅仅是所选类别
让自己感到困惑
答案 0 :(得分:2)
我想你的问题是由于你构建if-else条件的复杂方式。一个更简单的方法是按照以下方式进行...
// Get the input variables
$keywords = (isset($_POST['keywords']) && strlen($_POST['keywords']) > 0) ? $_POST['keywords'] : null;
$location = (isset($_POST['location']) && strlen($_POST['location']) > 0) ? $_POST['location'] : null;
$catno = (isset($_POST['industry']) && strlen($_POST['industry']) > 0) ? $_POST['industry'] : null;
$whereUsed = false;
$whereString = "";
$resultsfor = "";
// Add to the WHERE clause if keywords exists.
if ($keywords !== null) {
if (! $whereUsed) {
$whereString .= 'WHERE ';
$whereUsed = true;
} else {
$whereString .= ' AND ';
}
$whereString .= "(title LIKE '%{$keywords}%' OR description LIKE '%{$description}%')";
if ($catno === null) {
$resultsfor .= $keywords;
}
}
// Add to the WHERE clause if catno exists.
if ($catno !== null) {
if (! $whereUsed) {
$whereString .= 'WHERE ';
$whereUsed = true;
} else {
$whereString .= ' AND ';
}
$whereString .= "(catno = '{$catno}')";
$resultsfor .= $catno;
}
// Add to the WHERE clause if location exists.
if ($location !== null) {
if (! $whereUsed) {
$whereString .= 'WHERE ';
$whereUsed = true;
} else {
$whereString .= ' AND ';
}
$whereString .= "(location LIKE '%{$location}%')";
if ($catno === null && $keywords === null) {
$resultsfor = "{$location} jobs";
} else {
$resultsfor .= " jobs in {$location}";
}
}
// Build the SQL query using the WHERE clauses we've built up
$sqlQuery = "SELECT * FROM listings {$whereString} LIMIT 0, 50";
// Execute the query and fetch the response
$viewq = mysqli_query($con, $sqlQuery);
$view = mysqli_fetch_assoc($viewq);
您的原始代码中不需要if-else-if格式。您只是根据是否设置了变量来添加WHERE
子句......所以您应该这样做。
请注意,在您的代码中,在我的示例中,SQL查询容易受到SQL注入攻击。我强烈建议调查prepared statements。
答案 1 :(得分:1)
我在你的if语句中看到了一些错误。你应该测试从大多数特定到低限制。例如,您的第一个elseif
将永远不会发生,因为在这种情况下,第一个if
总是会出现。
答案 2 :(得分:1)
应该有九种组合来过滤您的查询。你的条件应该是
<div class="tp-cont" ng-style="{ 'background-image': 'url({{bgUrl}})' }">
答案 3 :(得分:0)
if (isset($_POST['keywords']) && isset($_POST['location']) &&
isset($_POST['industry'])) {
$keywords = $_POST['keywords'];
$location = $_POST['location'];
$catno = $_POST['industry'];
$viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`title` LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location` LIKE '%$location%') AND (`catno` = '$catno') LIMIT 0,50");
$resultsfor = $keywords . ' jobs in ' . $location;
} elseif (isset($_POST['keywords']) && isset($_POST['location']) &&
empty($_POST['industry'])) {
$keywords = $_POST['keywords'];
$location = $_POST['location'];
$viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`title`
LIKE '%$keywords%' OR `description` LIKE '%$keywords%') AND (`location`
LIKE '%$location%') LIMIT 0,50");
$resultsfor = $keywords . ' jobs in ' . $location;
} elseif (isset($_POST['keywords']) && empty($_POST['location']) &&
empty($_POST['industry'])) {
$keywords = $_POST['keywords'];
$viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`title`
LIKE '%$keywords%' OR `description` LIKE '%$keywords%') LIMIT 0,50");
$resultsfor = $_POST['keywords'];
} elseif (empty($_POST['keywords']) && isset($_POST['industry']) &&
empty($_POST['location'])) {
$industry = $_POST['industry'];
$viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE `catno` =
'$industry' LIMIT 0,50");
$resultsfor = $_POST['industry'] . ' jobs';
} elseif (empty($_POST['keywords']) && isset($_POST['industry']) &&
isset($_POST['location'])) {
$industry = $_POST['industry'];
$location = $_POST['location'];
$viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE (`catno` =
'$industry') AND (`location` LIKE '%$location%') LIMIT 0,50");
$resultsfor = $_POST['industry'] . ' jobs in ' . $location;
} elseif (empty($_POST['keywords']) && empty($_POST['industry']) &&
isset($_POST['location'])) {
$location = $_POST['location'];
$viewq = mysqli_query($con, "SELECT * FROM `listings` WHERE
(`location` LIKE '%$location%') LIMIT 0,50");
$resultsfor = $_POST['location'] . ' jobs';
}
$view = mysqli_fetch_assoc($viewq);
请试试这个
答案 4 :(得分:0)
我尝试最小化您的代码并检查它并告诉它是否正常
<?php
//Enter your code here, enjoy!
$query = "SELECT * FROM listings ";
$eQuery = "";
$resultsfor = "";
if(isset($_POST['keywords']) && !empty($_POST['keywords']){
$eQuery .= " (title LIKE '%".addslashes($_POST['keywords'])."%' OR description LIKE '%".addslashes($_POST['keywords'])."%') ";
$resultsfor .= $_POST['keywords'];
}
if(isset($_POST['location']) && !empty($_POST['location']){
$eQuery .= " (`location` LIKE '%".addslashes($_POST['location'])."%') ";
$resultsfor .= ' jobs in '.$location;
}
if(isset($_POST['industry']) && !empty($_POST['industry'])){
$eQuery .= " (`catno` = '".$_POST['industry']."')";
$resultsfor = (isset($_POST['keywords']) && (isset($_POST['location'])))? $_POST['keywords'] .' jobs in '.$_POST['location'] ;
}
$query = mysql_query($con, $query.$eQuery." LIMIT 0,50");
$view = mysqli_fetch_assoc($query);