php - 搜索引擎不适用于我的分页器

标签 php mysql pagination search-engine

<分区>

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

<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>

搜索后我得到消息:Undeclared variable: 5WHERE 正如我已经说过的,当我删除分页器时一切正常,搜索有效,但它不适用于启用分页器。

最佳答案

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 中被完全删除。使用 mysqlipdo反而。 And this is why you shouldn't use mysql_* functions .

关于php - 搜索引擎不适用于我的分页器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993918/

相关文章:

php - MySQL FROM_UNIXTIME

MySQL 动态 ORDER BY

javascript - AngularJS dirPaginate 仅过滤当前页面

php - 使用分页时是否需要使用 mysql_free_result 和 mysql_close? PHP

php - laravel-5.2 分页不起作用 - 未定义方法 stdClass::links() 错误

php - 如何使用 php 从 mysql 中正确检索数据?

php - 当表 2 id = x 时,Laravel 5 Eloquent 选择表 1 中与表 2 中没有关系的项目

javascript - Pusher 以 > 200ms 减慢 ajax 请求

php - Laravel 数据库不同字段之间的唯一键

php - 限制要获取的 RSS 提要数量