如何使用if isset提交按钮创建分页

时间:2017-12-10 16:28:53

标签: php mysql twitter-bootstrap pagination

如何使用if(isset($_POST['submit'])){

创建分页

首次展示时一切正常,但是当我在分页中点击下一页时,它会显示如下错误:

Notice: Undefined variable: modesearch in C:\xampp\htdocs\KaRomAshi\search.php on line 21 Notice: Undefined variable: searchstring in C:\xampp\htdocs\KaRomAshi\search.php on line 21 Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIKE '%%'' at line 1 in C:\xampp\htdocs\KaRomAshi\search.php:23 Stack trace: #0 C:\xampp\htdocs\KaRomAshi\search.php(23): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\KaRomAshi\search.php on line 23

我的isset和分页代码

if(isset($_POST['submit'])){
$modesearch = htmlentities($_POST['modesearch']);
$searchstring = htmlentities($_POST['searchstring']);
}
/*pagination code here*/
$limit = 1;
$query = ("SELECT * FROM post where $modesearch LIKE '%$searchstring%'");
$s = $db->prepare($query);
$s->execute();
$total_results = $s->rowCount();
$total_pages = ceil($total_results/$limit);
if (!isset($_GET['page'])) {
$page = 1;
} else{
$page = $_GET['page'];
}
$starting_limit = ($page-1)*$limit;
$show  = "SELECT * FROM post where $modesearch LIKE '%$searchstring%' ORDER BY date DESC LIMIT $starting_limit, $limit";
$r = $db->prepare($show);
$r->execute(); 
?>

显示此处数据的代码

<?php while ($value = $r->fetch(PDO::FETCH_ASSOC)): ?>
<!-- Start Main Posting Here -->
<article class="list-lyrics">
<h3 class="newupdate"><a href="lyric.php?idp=<?php echo $value['idpost'] ?>&link=<?php echo ($value['artis']); ?>-<?php echo ($value['title']); ?>"><?php echo html_entity_decode($value['title']); ?></a></h3>
<div class="artis-home">
<p class="general"><i class="fa fa-user-o" aria-hidden="true"></i> <a href="artis.php?ida=<?php echo $value['idpost'] ?>&link=<?php echo ($value['artis']); ?>"><?php echo html_entity_decode ($value['artis']); ?></a></p>
</div>
</article>
<?php endwhile; } ?>

我的基于分页的引导程序

<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="archives.php" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only">Previous</span>
</a>
</li>
<?php for ($page=1; $page <= $total_pages ; $page++):?>
<li class="page-item active">

</li>
<li class="page-item">
<a href='<?php echo "?page=$page"; ?>' class="links"><?php  echo $page; ?></a>
</li>
<?php endfor; ?> 
<li class="page-item">
<a class="page-link" href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
</nav> 

1 个答案:

答案 0 :(得分:0)

您必须将搜索字词附加到分页链接,但为此您必须更改代码以通过GET变量接受搜索字词,或者分页应基于在表单上执行POST来工作