【发布时间】:2025-12-25 15:25:15
【问题描述】:
我是 PHP 新手。我开发了一个新页面,显示表格中的所有结果。我还在页面顶部放置了一些过滤器以显示搜索结果。提交搜索结果后,它会显示过滤结果并带有适当的分页。但是当我点击分页号时。第 2 或第 3 页,它再次显示所有结果。我在 * 网站或其他网站上搜索了很多,但无法这样做。请帮助解决这个问题。我在这里发布了代码
// single page for show result and submit query "allcustomerdetails.php"
<form name="frmnamesd" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" onSubmit="return validated(this)" id = "frmnamesd" enctype="multipart/form-data">
<fieldset class="row1">
  <label>Search By:</label>
<br>
<label>Customer ID:</label>  
<input type="text" name="c_id" size = "8" onKeyPress="return isNumberKey(event)" value = "<?php echo isset($_POST['c_id']) ? $_POST['c_id'] : '';?>" /> 
<label>Name:</label>  
<input type="text" name="c_name" size = "10" value = "<?php echo isset($_POST['c_name']) ? $_POST['c_name'] : '';?>" />  
<label>Mobile:</label>  
<input type="text" name="c_mobile" onKeyPress="return isNumberKey(event)" size="10" value = "<?php echo isset($_POST['c_mobile']) ? $_POST['c_mobile'] : '';?>" />  
<label>Customer Type:</label>   
<select name="c_type">
<option value="<?php echo isset($_POST['c_type']) ? $_POST['c_type'] : '';?>" ><?php echo isset($_POST['c_type']) ? $_POST['c_type'] : "-select-";?></option>
<option value="IW">IW</option>
<option value="AMC">AMC</option>
<option value="SAC">SAC</option>
<option value="OW">OW</option>
<option value="PS">PS</option>
</select> 
<label>Date From/To:</label> 
<input type="date" name="c_from" value = "<?php echo isset($_POST['c_from']) ? $_POST['c_from'] : '';?>">
<input type="date" name="c_to" value = "<?php echo isset($_POST['c_to']) ? $_POST['c_to'] : '';?>" ><br><br>
     <label>Address:</label>        
<input type="text" name="c_address" size = "32" value = "<?php echo isset($_POST['c_address']) ? $_POST['c_address'] : '';?>" >  
<center><input type="Submit" name="submit" value="Submit">  <input type="reset" name="reset" value="reset"></center>
</fieldset>
</form>
<?php
$limit = 100;
if (isset($_GET["page"])) {
$page = $_GET["page"];
$page_offset = ($page-1) * $limit;
}
else {
$page_offset = 0;
$page = 1;
}
$query = "SELECT * FROM customer_details";
$querycount = "SELECT COUNT(*) FROM customer_details";
if(isset($_POST['submit'])) {
// define the list of fields
$fromdate = $_POST['c_from'];
$todate = $_POST['c_to'];
$fields = array('c_id','c_name','c_mobile', 'c_type','c_address');
$conditions = array();
// loop through the defined fields
foreach($fields as $field){
//$abc = $_POST[$field];
//die;
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "`$field` LIKE '%" . mysqli_real_escape_string($con, $_POST[$field]) . "%'";
}
}
if(isset($_POST['c_from']) && $_POST['c_to'] != '') {
// builds the query
$query .= " WHERE c_next_service between '$fromdate' and '$todate'";
$querycount .= " WHERE c_next_service between '$fromdate' and '$todate'";
if(count($conditions) > 0) {
$query .= " AND" . implode (' AND ', $conditions);
$querycount .= " AND" . implode (' AND ', $conditions);
$query .= " order by c_id desc LIMIT $page_offset, $limit";
$querycount .= " order by c_id desc";
}
else {
$query .= " order by c_id desc LIMIT $page_offset, $limit";
$querycount .= " order by c_id desc";
}
}
// if there are conditions defined
elseif(count($conditions) > 0) {
$query .= " WHERE" . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
$query .= " order by c_id desc LIMIT $page_offset, $limit" ;
$querycount .= " WHERE" . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
$querycount .= " order by c_id desc" ;
}
else {
// append the conditions
$query .= " order by c_id desc LIMIT $page_offset, $limit";
$querycount .= " order by c_id desc";
}
}
else {
$query .= " order by c_id desc LIMIT $page_offset, $limit";
$querycount .= " order by c_id desc";
}
$result = mysqli_query($con, $query);
$resultcount = mysqli_query($con,$querycount);
$count = mysqli_fetch_row($resultcount);
$totalrows = $count[0];
<?php if($_POST) {?>
<br><center><button onClick="history.go(-1)">Go Back</button></center>
<?php
}
$total_pages = ceil($totalrows/$limit);
$pagLink = "<div class='pagin'>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<a href='allcustomerdetail.php?page=".$i."'> ".$i."</a>";
};
?><br><br><center><?php echo $pagLink . "</div>";?><br><br></center>
【问题讨论】:
标签: php mysql post pagination