【发布时间】:2015-07-10 10:37:53
【问题描述】:
有没有更简单的方法来使用url变量过滤mysql结果?我编写了以下代码,它在一定程度上有效,我知道必须有一个更简单的方法,但我不确定从哪里开始。我宁愿更换这个,因为它只工作了一半。
$start=0;
$limit=3;
if(isset($_GET['pg']))
{
$pg=$_GET['pg'];
$start=($pg-1)*$limit;
}
else {
$pg = 1;
}
$sql = mysql_query($query);
if(isset($_GET['signage'])) {
$result = mysqli_query($conn,"SELECT * FROM pilotOperators WHERE signage='1' LIMIT $start, $limit");
}
elseif(isset($_GET['certifications'])) {
$certification = $_GET['certifications'];
$result = mysqli_query($conn,"SELECT * FROM pilotOperators WHERE certifications='$certification' LIMIT $start, $limit");
}
elseif(isset($_GET['state'])) {
$state = $_GET['state'];
$result = mysqli_query($conn,"SELECT * FROM pilotOperators WHERE state='$state' LIMIT $start, $limit");
}
elseif(isset($_GET['certifications'], $_GET['signage'])) {
$certification = $_GET['certifications'];
$signage = $_GET['signage'];
$result = mysqli_query($conn,"SELECT * FROM pilotOperators WHERE signage='$signage' AND certifications='$certification' LIMIT $start, $limit");
}
elseif(isset($_GET['certifications'], $_GET['signage'], $_GET['state'])) {
$certification = $_GET['certifications'];
$signage = $_GET['signage'];
$state = $_GET['state'];
$result = mysqli_query($conn,"SELECT * FROM pilotOperators WHERE signage='$signage' AND certifications='$certification' AND state='$state' LIMIT $start, $limit");
}
else {
$result = mysqli_query($conn,"SELECT * FROM pilotOperators LIMIT $start, $limit");
}
while($row = mysqli_fetch_array($result))
{
echo "\n <table border='0' class='resultTable' width='75%'> \n";
echo "<tr> \n";
echo "<td width='120px'>ID: </td> \n";
echo "<td>" . $row['id'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Name: </td> \n";
echo "<td>" . $row['name'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Phone: </td> \n";
echo "<td>" . $row['phone'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Alt. Phone: </td> \n";
echo "<td>" . $row['alt_phone'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Fax: </td> \n";
echo "<td>" . $row['fax'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Email: </td> \n";
echo "<td>" . $row['email'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Website: </td> \n";
echo "<td><a href='" . $row['website'] . "' target='_blank'>" . $row['website'] . "</a></td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>City: </td> \n";
echo "<td>" . $row['city'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>State: </td> \n";
echo "<td>" . $row['state'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Certifications: </td> \n";
echo "<td>" . $row['certifications'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Top Sign: </td> \n";
echo "<td>";
if($row['signage'] = 1) {
echo "Has Top Sign";
}
else {
echo "Top Sign Not Listed";
}
echo "</td> \n";
echo "</tr> \n";
echo "</table> \n\n";
}
$rows = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM pilotOperators"));
$total=ceil($rows/$limit);
echo "<div id='paginationLinks'> \n";
if($pg>1)
{
echo "<a href='?pg=".($pg-1)."' class='paginationButton'>PREVIOUS</a> \n";
}
if($pg!=$total)
{
echo "<a href='?pg=".($pg+1)."' class='paginationButton'>NEXT</a> \n";
}
echo "<ul class='page'> \n";
for($i=1;$i<=$total;$i++)
{
if($i==$pg) { echo "<li class='current'>".$i."</li> \n"; }
else { echo "<li><a href='?id=".$i."'>".$i."</a></li> \n"; }
}
echo "</ul> \n";
echo "</div> \n";
mysqli_close($con);
当我说这段代码有点工作时,请允许我解释一下。当我的网址中有多个变量时(即 ?signage=VALUE&certifications=VALUE),代码的行为就像它有 OR 代替 AND。
像这样:$result = mysqli_query($conn,"SELECT * FROM PilotOperators WHERE signage='$signage' ORcertifications='$certification' LIMIT $start, $limit");
而不是这个:$result = mysqli_query($conn,"SELECT * FROM PilotOperators WHERE signage='$signage' AND certificates='$certification' LIMIT $start, $limit");
如果我的 URL 包含以下 &signage=1&certifications=washington,它不仅向我显示具有这两者的结果,而且向我显示具有华盛顿认证或 signage=1 的人的结果。
我没有太多要传递的变量。应该只有3,但我的方法看起来很复杂。
我可以做些什么来使这更简单,为什么我的 AND 在尝试过滤结果时表现得像 OR。
【问题讨论】:
-
当你尝试直接对 MySQL 运行这些查询时会发生什么(使用 MySQL 工作台或类似的东西)
-
变量都是通过checkbox和select选项传递的。除非他们编辑 URL 哈哈,否则不用担心用户输入。不过我会调查一下,因为我还有其他需要验证的表单。
-
The variables are all passed by checkboxes and select options不代表不能独立于表单提交数据(通过更改网址自动或手动提交) -
Maximus 完全按照我说的做。它确实过滤了结果,但它基本上忽略了 AND 条件并表现得像他们说“OR”拉取具有部分参数而不是全部参数的结果。
标签: php mysql variables filter