Ajax&;PHP:MySQL查询不获取任何行
我设置了3个组合框来过滤MySQL数据库中的结果。加载时,所有结果都以正确的顺序显示,但当我尝试从3个组合框中选择任意1个,或从3个组合框中选择任意2个时,不会显示任何结果(行)。如果我选择所有3个组合框,则显示结果 我希望有人能找出我的查询代码出了什么问题。我什么都试过了,似乎没发现有什么问题。我也不知道我是不是在用最好的方式做这件事。我对PHP相当陌生,所以我不知道所有可用的不同方法 PHP:Ajax&;PHP:MySQL查询不获取任何行,php,mysql,ajax,jquery,Php,Mysql,Ajax,Jquery,我设置了3个组合框来过滤MySQL数据库中的结果。加载时,所有结果都以正确的顺序显示,但当我尝试从3个组合框中选择任意1个,或从3个组合框中选择任意2个时,不会显示任何结果(行)。如果我选择所有3个组合框,则显示结果 我希望有人能找出我的查询代码出了什么问题。我什么都试过了,似乎没发现有什么问题。我也不知道我是不是在用最好的方式做这件事。我对PHP相当陌生,所以我不知道所有可用的不同方法 PHP: //Define Refine Data Values $imgFamily = $_GET['i
//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";
}
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() {
}
});
}
//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>";
}
我很确定问题出在PHP上,因为javascript端没有调用错误,当我只有两个组合框时,一切都正常,但我会发布我的jQuery Ajax查询,以防出现问题
Ajax:
//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";
}
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() {
}
});
}
//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>";
}
您没有得到结果的原因是因为您正在使用所说的
和运算符
如果其A
和B
和C
则获取结果,因此当您从3个组合框值中选择1个或从3个组合框值中选择2个时,该条件失败,并且您没有得到任何结果
范例
if(A && B && C)
// do something
然后选择两个值B
和C
,因此条件返回false真是一团糟。你一定要把它清理干净:
$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";
在真实情况下,您会说“如果值不等于1或值不为空”。但是,如果该值等于1,则该值不为null,因此为true。如果该值为null,则它不等于1,因此为true
问题:
$imgFamilyTrue = (($imgFamily != 1) || ($imgFamily != null));
$imgFamilyFalse = (($imgFamily == 1) || ($imgFamily == null));
修正:
我无法从上面的答案中选择一个“正确”的答案,因为Marc B和James的答案都有助于创建一个工作代码
单凭詹姆斯的回答就可以更正原始代码,然而马克B极大地帮助清理了代码,提高了效率。这两者都是道具
除了他们的建议之外,我还必须将$where_子句[]=“1=1”
包装在一个if
条件下,在这个条件下,Ajax没有可用的数据
我还必须将mysql\u real\u escape\u字符串($\u 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 = (($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";
}
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() {
}
});
}
//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>";
}
//定义优化数据值
$imgFamily=$\u GET['imgFamily'];
$imgClass=$\u GET['imgClass'];
$imgGender=$_GET['imgGender'];
//将优化值定义为True或False
$imgFamilyTrue=$imgFamily!=1&&$imgFamily!=null;
$imgFamilyFalse=!$imgFamilyTrue;
$imgClassTrue=$imgClass!=1&&$imgClass!=null;
$imgClassFalse=!$imgClassTrue;
$imgGenderTrue=$imgGender!=1&&$imgGender!=null;
$imgGenderFalse=!$imgGenderTrue;
//where子句
$where_子句=数组();
if($imgFamilyFalse&&$imgClassFalse&&$imgGenderFalse){
$where_子句[]=“1=1”;//默认不执行任何操作子句
}
如果($imgFamilyTrue){
$where_子句[]='imgffamily='。“.”。mysql\u real\u escape_字符串($\u GET['imgffamily'])。“”;
}
如果($imgClassTrue){
$where_子句[]='imgClass='。“.”。mysql_real_escape_字符串($\u GET['imgClass'])。“”;
}
如果($imgGenderTrue){
$where_子句[]='imgGender='。“.”。mysql_real_escape_字符串($\u GET['imgGender'])。“”;
}
包括“db.php”;
$子句=内爆('AND',$where_子句);
$query_pag_data=“从图像中选择imgURL、imgTitle,其中$clause按imgDate DESC排序”;
$result_pag_data=mysql_query($query_pag_data)或die('mysql Error'.mysql_Error());
$num_rows=mysql_num_rows($result_pag_data);
if(!$result\u pag\u数据){
echo“无法从数据库检索信息。”;
}else if($num_rows==0){
echo“抱歉,没有与您的请求匹配的项目。”;
}否则{
echo“”;
而($row=mysql\u fetch\u assoc($result\u pag\u data)){
回声“”;
}
回声“
”;
}
我猜你指的是if else
条件?我明白你的意思,但是那些错误的变量,即$imgFamilyFalse
在上面被定义为==1
或|
==null
,如果不在客户端选择它们,那么这不是解决了问题吗你建议的问题?这就是我喜欢堆栈溢出的原因!我知道我使用的方法很混乱,但我不知道我可以使用什么其他方法。我现在必须运行,但我会测试一下,让你知道。非常感谢Marc!不过你一定要添加一些清理。在查询中使用原始用户提供的数据字符串从来都不是一个好主意。我没有把它放进去,所以查询很容易受到sql注入攻击。请参阅此链接,了解@Marc B Oh的清理方法,您能否澄清一下(因为这是我不熟悉的语法)。array()
你提到的;我可以在那里输入一些特定的内容吗?我把$\u GET
定义放在哪里?对不起,我只是想更好地理解你在做什么。@Marc B Th数据来自一个组合框,我计划用数据库列中不同的值填充该组合框,那么我这样做是否正确所以,没有必要进行消毒?很有意思。我一直理解|
是一种情况还是另一种情况。但我明白你的意思。难道&
不一定要使这两个条件都为真吗?在这种情况下,当没有选择结果时,值要么是1
要么是null
。这两种情况都不会发生。但如果我这样做,请纠正我误解。对,但你使用的是否定词。imgFamily==null满足imgFamily!=1,imgFamily==1满足imgFamily!=null,看到了吗?所以对于imgFamily的任何值,连接的or条件都返回true。上面的条件“mess”给了我噩梦…@Jakub相信我,它也给了我噩梦。但是感谢Individual的帮助下面,我再也不会做这样的噩梦了!这就是结果