我设置了3个组合框来过滤来自MySQL数据库的结果。在加载时,所有的结果都按正确的顺序显示,但当我尝试从3个组合框中选择任意1个或从3个组合框中选择任意2个时,就不会显示结果(行)。如果我选择所有3个组合框,则结果将显示。
我希望有人能找出我的查询代码出了什么问题。我什么都试过了,似乎找不到任何问题。我也想知道我是否在以最好的方式做这件事。我对PHP相当陌生,所以我不知道所有可用的不同方法。
:
//Define Refine Data Values
$imgFamily = $_GET['imgFamily'];
$imgClass = $_GET['imgClass'];
$imgGender = $_GET['imgGender'];
//Define Refine Values as True of False
$imgFamilyTrue = (($imgFamily != 1) || ($imgFamily != null));
$imgFamilyFalse = (($imgFamily == 1) || ($imgFamily == null));
$imgClassTrue = (($imgClass != 1) || ($imgClass != null));
$imgClassFalse = (($imgClass == 1) || ($imgClass == null));
$imgGenderTrue = (($imgGender != 1) || ($imgGender != null));
$imgGenderFalse = (($imgGender == 1) || ($imgGender == null));
include"db.php";
//Database queries based on refine selections
if($imgFamilyFalse && $imgClassFalse && $imgGenderFalse) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` ".
"ORDER BY `imgDate` DESC";
} else if($imgFamilyTrue && $imgClassTrue && $imgGenderTrue) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` WHERE imgFamily='$imgFamily' AND imgClass='$imgClass' AND imgGender='$imgGender' ".
"ORDER BY `imgDate` DESC";
} else if($imgFamilyTrue && $imgClassFalse && $imgGenderFalse) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` WHERE imgFamily='$imgFamily' ".
"ORDER BY `imgDate` DESC";
} else if($imgFamilyFalse && $imgClassTrue && $imgGenderFalse) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` WHERE imgClass='$imgClass' ".
"ORDER BY `imgDate` DESC";
} else if($imgFamilyFalse && $imgClassFalse && $imgGenderTrue) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` WHERE imgGender='$imgGender' ".
"ORDER BY `imgDate` DESC";
} else if($imgFamilyFalse && $imgClassTrue && $imgGenderTrue) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` WHERE imgClass='$imgClass' AND imgGender='$imgGender' ".
"ORDER BY `imgDate` DESC";
} else if($imgFamilyTrue && $imgClassFalse && $imgGenderTrue) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` WHERE imgFamily='$imgFamily' AND imgGender='$imgGender' ".
"ORDER BY `imgDate` DESC";
} else if($imgFamilyTrue && $imgClassTrue && $imgGenderFalse) {
$query_pag_data = "SELECT `imgURL`,`imgTitle` FROM `images` WHERE imgFamily='$imgFamily' AND imgClass='$imgClass' ".
"ORDER BY `imgDate` DESC";
}我很确定问题出在PHP上,因为javascript端没有调用错误,而且当我只有两个组合框时,一切都正常,但是我会发布我的jQuery Ajax查询,以防问题发生在这一端。
Ajax:
function loadData(imgFamily, imgClass, imgGender){
$.ajax
({
type: "GET",
url: "filter_test.php",
data: {imgFamily:imgFamily, imgClass:imgClass, imgGender:imgGender},
success: function(msg) {
$("#gallery_container").html(msg);
},
error: function(jqXHR, textStatus, errorThrown) {
},
complete: function() {
}
});
}发布于 2011-08-25 17:18:58
我无法从上面的答案中选择一个“正确”的答案,因为Marc和James的答案都有助于创建工作代码。
James的答案本身就可以纠正原来的代码,但是Marc极大地帮助了代码的清理,并提高了代码的效率。两个人的道具。
除了他们的建议之外,我还必须将$where_clauses[] = "1=1"包装在if条件下,因为那里没有来自Ajax的可用数据。
我还必须用单引号包装mysql_real_escape_string($_GET[],这样才能在MySQL查询中使用。
我的最后一个PHP:
//Define Refine Data Values
$imgFamily = $_GET['imgFamily'];
$imgClass = $_GET['imgClass'];
$imgGender = $_GET['imgGender'];
//Define Refine Values as True of False
$imgFamilyTrue = $imgFamily != 1 && $imgFamily != null;
$imgFamilyFalse = !$imgFamilyTrue;
$imgClassTrue = $imgClass != 1 && $imgClass != null;
$imgClassFalse = !$imgClassTrue;
$imgGenderTrue = $imgGender != 1 && $imgGender != null;
$imgGenderFalse = !$imgGenderTrue;
//where clauses
$where_clauses = array();
if ($imgFamilyFalse && $imgClassFalse && $imgGenderFalse) {
$where_clauses[] = "1=1"; // default do-nothing clause
}
if ($imgFamilyTrue) {
$where_clauses[] = 'imgFamily=' . "'" . mysql_real_escape_string($_GET['imgFamily']) . "'";
}
if ($imgClassTrue) {
$where_clauses[] = 'imgClass=' . "'" . mysql_real_escape_string($_GET['imgClass']) . "'";
}
if ($imgGenderTrue) {
$where_clauses[] = 'imgGender=' . "'" . mysql_real_escape_string($_GET['imgGender']) . "'";
}
include"db.php";
$clause = implode(' AND ', $where_clauses);
$query_pag_data = "SELECT imgURL, imgTitle FROM images WHERE $clause ORDER BY imgDate DESC";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$num_rows = mysql_num_rows($result_pag_data);
if(!$result_pag_data) {
echo "Cannot retrieve information from database.";
} else if($num_rows == 0) {
echo "<div id='no_result'>Sorry, there are no items matching your request.</div>";
} else {
echo "<ul class='new_arrivals_gallery'>";
while($row = mysql_fetch_assoc($result_pag_data)) {
echo "<li><a target='_blank' href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
}
echo "</ul>";
}发布于 2011-08-24 17:37:25
多么可怕的烂摊子。你绝对应该把它清理干净:
$where_clauses = array();
$where_clauses[] = "1=1"; // default do-nothing clause
if ($_GET['imgFamily']) {
$where_clauses[] = "imgFamily='$imgFamily'";
}
if ($_GET['imgClass']) {
$where_clauses[] = "imgClass='$imgClass'";
}
if ($_GET['imgGender']) {
$where_clauses[] = "imgFamily='$imgFamily'";
}
$clause = implode(' AND ', $where_clauses);
$sql = "SELECT imgURL, imgTitle FROM images WHERE $clause ORDER BY imgDate DESC";发布于 2011-08-24 17:41:58
在您的真实情况下,您可以说“如果值不等于1或值不为null”。但是,如果该值等于1,则不为空,因此您将得到真值。如果该值为null,则它不等于1,因此得到了真值。
问题:
$imgFamilyTrue = (($imgFamily != 1) || ($imgFamily != null));
$imgFamilyFalse = (($imgFamily == 1) || ($imgFamily == null));修正:
$imgFamilyTrue = $imgFamily != 1 && $imgFamily != null;
$imgFamilyFalse = !$imgFamilyTrue;https://stackoverflow.com/questions/7179652
复制相似问题