搜索引擎不能与我的分页器一起使用

时间:2016-05-03 00:52:03

标签: php mysql pagination search-engine

我在PHP中非常棒。我做了一个php脚本,从mysql表中获取数据并将其存储在文本框中,然后我可以编辑文本框并单击更新按钮来更新mysql。我在我的脚本中添加了paginator,因为结果太多了。现在我正在尝试添加搜索引擎。这是我的代码:

<html>
<head>
<title>TITLE</title>
</head>
<style>
/* #container {
display:inline-block;width:100%;float:left;clear:left
}
table{
    width:100%;
}
td{
    border: 0;
}
td {width:100%;float:left;clear:left}
th {visibility:hidden;} */
</style>
<body>
<?php
$num_rec_per_page=5;
$con = mysql_connect("localhost","root","pass");
if (!$con) {
die("connE: " . mysql_error());
}
mysql_select_db("database_name",$con);

if(isset($_POST['update'])){
$UpdateQuery = "UPDATE users SET 
username='$_POST[username]',
password='$_POST[password]'
WHERE username='$_POST[hidden]'"; // etc, etc ... i deleted all the stuff cause it's too many

mysql_query($UpdateQuery, $con);    
};

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 

$sql = "SELECT * FROM users LIMIT $start_from, $num_rec_per_page";

if (isset($_GET['search'])) {
    $search_term = mysql_real_escape_string($_GET['search_box']);
    $sql .= "WHERE username = '{$search_term}'";
};

$myData = mysql_query($sql, $con) or die (mysql_error());

echo "<form name='search_form' method='GET' action='users.php'>
Search: <input type='text' name='search_box' value='Search' />
<input type='submit' name='search' value='Search'>
</form>";

echo "
<div id='container'>
<table>
<tr>
<th>Username</th>
<th>Password</th>
</tr>"; // etc, etc ... i deleted all the stuff cause it's too many
while($record = mysql_fetch_array($myData)){
echo "<form action=users.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=username value='" . $record['username'] . "' </td>";
echo "<td>" . "<input type='text' name='password' value='" . $record['password'] . "' </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['username'] . "' </td>";
echo "<td>" . "<input type='submit' name='update' value='update'" . " </td>";
echo "</tr>"; // etc, etc ... i deleted all the stuff cause it's too many
echo "</form>";
echo "<br />";
}
echo "</table>";

$sql = "SELECT * FROM users";
$myData = mysql_query($sql, $con);
$total_records = mysql_num_rows($myData);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);

echo "</div>";

echo "<a href='users.php?page=1'>"."<img style='margin-right:10px;margin-top:-3px;' src='img/prev.png'>"."</a> "; // Goto 1st page 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='users.php?page=".$i."'>&nbsp;".$i."&nbsp;</a> "; 
}; 

echo "<a href='users.php?page=$total_pages'>"."<img style='margin-left:11px;margin-top:-3px;' src='img/next.png'>"."</a> "; // Goto last page

mysql_close($con);
?>

</body>
</html>

在搜索时我得到消息:未声明的变量:5WHERE 正如我已经说过的,当我删除分页符时它一切正常,搜索工作正常,但它不能使用分页器。

1 个答案:

答案 0 :(得分:1)

LIMIT子句应该在WHERE子句之后。所以你的代码应该是这样的:

// your code

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page;

$sql = "SELECT * FROM users";

if (isset($_GET['search'])) {
    $search_term = mysql_real_escape_string($_GET['search_box']);
    $sql .= " WHERE username = '{$search_term}'";
}

$sql .= " LIMIT $start_from, $num_rec_per_page";

$myData = mysql_query($sql, $con) or die (mysql_error());

// your code

旁注:不要使用mysql_*函数,从PHP 5.5开始不推荐使用它们,并且在PHP 7.0中完全删除它们。请改用mysqlipdoAnd this is why you shouldn't use mysql_* functions