搜索数据并将数据保存在字段中

问题描述:

请检查此代码以解决问题。搜索数据并将数据保存在字段中

  1. 当我按名称或年龄搜索数据时,我会在页面的前10行中得到结果,但是当我移动到下一页以查看结果时,它会返回到所有结果。我怎样才能去下一页没有我输入的名字或年龄没有我的数据没有删除 或我在其中搜索的网页停留。
  2. 当我搜索我有年龄空列问题的所有数据,如果年龄为空并没有出现我需要看到所有的数据,如果是空或不是,我进入int类型年龄

<table class="tab"> 
 
     <thead> 
 
\t <tr> 
 
\t <th colspan="5" class="table-title"> 
 
\t <h3>Search</h3> 
 
\t </th> 
 
\t </tr> 
 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="searchform"> 
 
    <tr class="trr"> 
 
    <th class="thh"> 
 
    Full Name 
 
    </th> 
 
    <th class="thh"> 
 
    Age 
 
    </th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr class="trr"> 
 
    <td class="tdd"> 
 
     <input type="text" autocomplete="off" style="width:150px;" name="name"> 
 
     </td> 
 
    <td class="tdd"> 
 
     <input type="text" autocomplete="off" style="width:50px;" name="age"> 
 
\t </td> 
 
    <td class="tdd"> 
 
\t <input type="submit" name="submit" value="Search"> 
 
\t </td> 
 
    </tbody> 
 
    </table> 
 
    </form> 
 
    <br> 
 
    </body> 
 
<?php 
 
\t if (!empty($_POST['name'])){ 
 
\t $name=$_POST['name']; 
 
\t } 
 
\t if (!empty($_POST['age'])){ 
 
\t  $age=$_POST['age']; 
 
\t } 
 

 
$con=mysqli_connect("localhost","name","password","db"); 
 
$ch = 'SET CHARACTER SET utf8'; 
 
\t mysqli_query($con,$ch); 
 
if (mysqli_connect_errno()) 
 
{ 
 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
 
} 
 

 
$tbl_name="table"; //your table name 
 
// How many adjacent pages should be shown on each side? 
 
$adjacents = 3; 
 
    
 
/* 
 
    First get total number of rows in data table. 
 
    If you have a WHERE clause in your query, make sure you mirror it here. 
 
*/ 
 

 
$resul = mysqli_query($con,"SELECT ID,full_name,age FROM arrested WHERE 
 
\t \t \t \t \t \t \t \t \t full_name LIKE '%$name%' AND 
 
\t \t \t \t \t \t \t \t \t age LIKE '%$age%'"); 
 

 
$num_rows = mysqli_num_rows($resul); 
 
$total_pages =$num_rows; 
 

 
/* Setup vars for query. */ 
 
$targetpage = "detainees.php"; //your file name (the name of this file) 
 
$limit = 10; 
 
    //how many items to show per page 
 
if(isset($_GET['page'])) 
 
{$page = $_GET['page'];} 
 
else{$page =0;} 
 
if($page) 
 
    $start = ($page - 1) * $limit; //first item to display on this page 
 
else 
 
    $start = 0;  //if no page var is given, set start to 0 
 
    
 
/* Setup page vars for display. */ 
 
if ($page == 0) $page = 1;  //if no page var is given, default to 1. 
 
$prev = $page - 1;  //previous page is page - 1 
 
$next = $page + 1;  //next page is page + 1 
 
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages/items per page, rounded up. 
 
$lpm1 = $lastpage - 1;  //last page minus 1 
 
    
 
/* 
 
    Now we apply our rules and draw the pagination object. 
 
    We're actually saving the code to a variable in case we want to draw it more than once. 
 
*/ 
 
$pagination = ""; 
 
if($lastpage > 1) 
 
{ 
 
    $pagination .= "<div class=\"pagination\">"; 
 
    //previous button 
 
    if ($page > 1) 
 
    $pagination.= "<a href=\"$targetpage?page=$prev\">Previous</a>"; 
 
    else 
 
    $pagination.= "<span class=\"disabled\">Previous</span>"; 
 
    
 
    //pages 
 
    if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up 
 
    { 
 
    for ($counter = 1; $counter <= $lastpage; $counter++) 
 

 
    { 
 
    if ($counter == $page) 
 
    $pagination.= "<span class=\"current\">$counter</span>"; 
 
    else 
 
    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";  
 
    } 
 
    } 
 
    elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some 
 
    { 
 
    //close to beginning; only hide later pages 
 
    if($page < 1 + ($adjacents * 2)) 
 
    { 
 
    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) 
 
    { 
 
    if ($counter == $page) 
 
     $pagination.= "<span class=\"current\">$counter</span>"; 
 
    else 
 
     $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";  
 
    } 
 
    $pagination.= "<b class='dot'> . . . </b>"; 
 
    $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; 
 
    $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; 
 
    } 
 
    //in middle; hide some front and some back 
 
    elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) 
 
    { 
 
    $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; 
 
    $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; 
 
    $pagination.= "<b class='dot'> . . . </b>"; 
 
    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) 
 
    { 
 
    if ($counter == $page) 
 
     $pagination.= "<span class=\"current\">$counter</span>"; 
 
    else 
 
     $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";  
 
    } 
 
    $pagination.= "<b class='dot'> . . . </b>"; 
 
    $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>"; 
 
    $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>"; 
 
    } 
 
    //close to end; only hide early pages 
 
    else 
 
    { 
 
    $pagination.= "<a href=\"$targetpage?page=1\">1</a>"; 
 
    $pagination.= "<a href=\"$targetpage?page=2\">2</a>"; 
 
    $pagination.= "<b class='dot'> . . . </b>"; 
 
    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) 
 
    { 
 
    if ($counter == $page) 
 
     $pagination.= "<span class=\"current\">$counter</span>"; 
 
    else 
 
     $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";  
 
    } 
 
    } 
 
    } 
 
    
 
    //next button 
 
    if ($page < $counter - 1) 
 
    $pagination.= "<a href=\"$targetpage?page=$next\">Next</a>"; 
 
    else 
 
    $pagination.= "<span class=\"disabled\">Next</span>"; 
 
    $pagination.= "</div>\n"; 
 
} 
 

 
/* Get data. */ 
 
$resul = mysqli_query($con,"SELECT ID,full_name,age FROM arrested WHERE 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t full_name LIKE '%$name%' AND 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t age LIKE '%$age%' 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t ORDER BY ID ASC LIMIT $start, $limit"); 
 
    
 
echo'<table class="table-fill">'; 
 
\t echo'<thead><tr class="data"> 
 
<th class="text-left">ID</th> 
 
<th class="text-left">Full Name</th> 
 
<th class="text-left">Age</th> 
 
</tr></thead> 
 
<tbody class="table-hover">'; 
 
while($x= mysqli_fetch_object($resul)) 
 
\t { 
 

 
\t echo '<tr class="data">'; 
 
    \t echo '<td class="data">'; 
 
\t echo $x->ID; 
 
\t echo "</td>"; 
 
    \t echo '<td class="data">'; 
 
\t echo $x->full_name; 
 
\t echo "</td>"; 
 
    \t echo '<td class="data">'; 
 
\t echo $x->status; 
 
\t echo "</td>"; 
 
\t 
 
    \t echo "</tr>"; 
 
\t \t } 
 
\t \t print "</table>"; 
 
mysqli_close($con); 
 
?> 
 
<?=$pagination ?>

使用会话($ _SESSION),就像这个例子:

<?php 
session_start(); 

if (isset($_POST['var']) && !empty($_POST['var'])) 
{ 
    $var = $_POST['var']; 
    $_SESSION['var'] = $var; 
} 
elseif (isset($_SESSION['var'])) 
{ 
    $var = $_SESSION['var']; 
} 
?> 

变量设置$ _SESSION将b直到会话结束(通常在浏览器关闭时)。