添加不同的sql查询条件时分页Php不起作用
您好,我的分页系统遇到了一个问题,如果我列出mysql表中的结果,它可以正常工作,但是如果我在SQL查询中添加一些条件,如“AND”“this column”“other column”,脚本会在第一页正确显示结果,当我选择第二个页面而不是显示26 forward结果的第二部分时,它开始了一个新的分页,并且它显示了从一开始的所有内容,而没有在查询中添加contions。以下是查询分页的代码:添加不同的sql查询条件时分页Php不起作用,php,pagination,Php,Pagination,您好,我的分页系统遇到了一个问题,如果我列出mysql表中的结果,它可以正常工作,但是如果我在SQL查询中添加一些条件,如“AND”“this column”“other column”,脚本会在第一页正确显示结果,当我选择第二个页面而不是显示26 forward结果的第二部分时,它开始了一个新的分页,并且它显示了从一开始的所有内容,而没有在查询中添加contions。以下是查询分页的代码: //This gets all the other information from the form
//This gets all the other information from the form
$ciudad=$_POST['ciudad'];
$tipo=$_POST['tipo'];
$con=mysqli_connect();
// Check connection
$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
$sql .= " AND ciudad = '$ciudad' ";
}
if (!empty($tipo)) {
$sql .= " AND tipo= '$tipo' ";
}
if (!$result = mysqli_query($con,$sql))
{
die("Error: " . mysqli_error($con));
}
$per_page =25;//define how many games for a page
$count = mysqli_num_rows($result);
$pages = ceil($count/$per_page);
if(!isset($_GET['page']) || $_GET['page']=="") {
$page="1";
} else {
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql = "SELECT * FROM cursos WHERE 1 LIMIT $start,$per_page";
?>
这是生成的页面链接的代码:
<?php
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><a href="search_cursos.php?page=<?php echo $i;?>"><?php echo $i;?></a></li>
<?php
}
?>
如果设置了城市和类型,那么SQL将有两个order by实例。。。您应该在if语句之后添加order by。如果$ciudad和$tipo都不是空的,那么您的执行查询将如下所示:
SELECT * FROM cursos WHERE 1 AND ciudad = '$ciudad' ORDER BY id DESC AND tipo= '$tipo' ORDER BY id DESC
如果我没有弄错的话,应该是这样的:
SELECT * FROM cursos WHERE 1 AND ciudad = '$ciudad' AND tipo= '$tipo' ORDER BY id DESC
我要做的是改变这一点:
$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
$sql .= " AND ciudad = '$ciudad' ORDER BY id DESC ";
}
if (!empty($tipo)) {
$sql .= " AND tipo= '$tipo' ORDER BY id DESC ";
}
这也是:
$sql = "SELECT * FROM cursos WHERE 1 ";
if (!empty($ciudad)) {
$sql .= "AND ciudad= '$ciudad' ";
if (!empty($tipo)) {
$sql .= "AND tipo= '$tipo' ";
}
$sql .= "ORDER BY id DESC ";
}
我还有一个链接,它可能会帮助您进行分页
以下两个问题:
- 在下一页中不再选择其他筛选器(
$\u POST
将为空)
- 与分页相关的说明,其中在查询后计算(显然,无法使用这些参数)
您可以将额外的查询条件存储在会话中,或者将其作为参数添加到“下一页链接”中,或者将链接转换为提交表单(这可能是最好的选择)
打印选择并在控制台中手动探测您当前的代码容易受到SQL注入攻击。您可能希望查看准备好的语句:此页面位于sevrer保护目录中,因此只有管理员才能看到它。所以不用担心SQL注入附件,谢谢你的评论谢谢我会解决的,但一般来说,当我点击第二页时,这并不能解决问题,该查询列出了所有结果,而不是指定的结果。我确实从查询中删除了ORDER选项,因为这不是真正的问题。感谢您的评论。主要问题解决后,我会考虑它。如果我选择链接选项,它会显示正确的页数,但在每个页面上显示相同的结果,在每个页面中,它都会显示所有结果,而不是25个(例如,如果在执行查询后添加限制X,Y
,那么…;)我只面临一个问题,即结果未被第二个参数$tipo过滤,但我将尝试找出原因。非常感谢你!
<li id="<?php echo $i;?>"><a href="search_cursos.php?page=<?php echo $i.'&ciudad='.$ciudad.'&tipo='.$tipo; ?>"><?php echo $i;?></a></li>
//This gets all the other information from the form
$ciudad=$_REQUEST['ciudad'];
$tipo=$_REQUEST['tipo'];
$con=mysqli_connect();
// Check connection
$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
$sql .= " AND ciudad = '$ciudad' ";
}
if (!empty($tipo)) {
$sql .= " AND tipo= '$tipo' ";
}
// PAGINATION MOVED UP
$per_page =25;//define how many games for a page
$count = mysqli_num_rows($result);
$pages = ceil($count/$per_page);
if(empty($_GET['page'])) {
$page="1";
} else {
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql .= ' LIMIT '.$start.','.$per_page;
if (!$result = mysqli_query($con,$sql))
{
die("Error: " . mysqli_error($con));
}
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><a href="search_cursos.php?page=<?php echo $i.'&ciudad='.$ciudad.'&tipo='.$tipo; ?>"><?php echo $i;?></a></li>
<?php
}
?>