ELSEIF搜索查询不工作
我有一个有3个要素 关键字,行业(下拉)&位置搜索表单ELSEIF搜索查询不工作
我想创建搜索功能,如果所有的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);
会很感激一些帮助,我已经花了大量的时间在这个 预先感谢
**它拉的数据虽然不正确,如果我搜索仓库(休假行业下拉空白),但设置位置莱斯特它仍然会查询所有的结果不莱斯特 如果我从下拉菜单中选择一个行业并保留其他领域的空白,我得到了所有类别的所有工作不仅仅是类别中选择
获取自己弄得现在
我想你的问题是由于你构造你的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。
我看到一些erros在你的if语句中。你应该从大多数特定测试到低限制。例如,您的第一个elseif
永远不会发生,因为在这种情况下,第一个if
总会发生。
已经更新了我上面的帖子,还我以前if语句&不elseif语句,但仍然是相同的问题 –
应该有9个组合来过滤您的查询。你的条件应该是
if(isset($_POST['keywords']) && !isset($_POST['location']) && !isset($_POST['industry'])){}
elseif(isset($_POST['keywords']) && !isset($_POST['location']) && isset($_POST['industry'])){}
elseif(isset($_POST['keywords']) && isset($_POST['location']) && !isset($_POST['industry'])){}
elseif(isset($_POST['keywords']) && isset($_POST['location']) && isset($_POST['industry'])){}
elseif(!isset($_POST['keywords']) && !isset($_POST['location']) && isset($_POST['industry'])){}
elseif(!isset($_POST['keywords']) && isset($_POST['location']) && !isset($_POST['industry'])){}
elseif(!isset($_POST['keywords']) && isset($_POST['location']) && isset($_POST['industry'])){}
我刚刚编辑我的代码,并删除isset和替换为!空,也是空的(在需要和工作太 –
很高兴帮助:) – Virushabadoss
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);
请尽量将
我试图减少你的代码,并检查这一点,并告诉我们,如果它的工作原理正确与否
<?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);
优秀的汤姆! 非常感谢..我在复杂的事情,那通常分崩离析 也关于SQL注入..我同意,虽然用户查询数据库除SELECT外没有其他特权..没有更新或任何其他:)也是用户没有其他数据库的访问 但我100%同意准备好的语句更好 –
没问题克里斯。从代码中退后一步,思考你实际上想要做什么,而不是直接跳入代码通常是有用的。 –
1 last quick q .. 经过我的查询我有$ resultsfor(请参阅我的文章) 我如何将这个代码与您提供的代码放在一起? –