gpt4 book ai didi

php - PHP/MySQL 搜索引擎的 session 问题

转载 作者:行者123 更新时间:2023-11-29 14:22:33 25 4
gpt4 key购买 nike

这里是 PHP/MySQL 新手。

我创建了一个基本的搜索引擎,用于查询包含多个值的 MySQL 表。但是,搜索表单具有多个搜索按钮,以便限制基于单一值类型的搜索(在本例中,按区域或震级,因为我们正在处理地震)

初始搜索和结果显示顺利进行,但是当我尝试使用分页链接转到结果的另一页时,它无法拉出下一组结果,并且当我单击时我的浏览器(我使用的是 Firefox,但我会在其他浏览器中测试这一点), session 已过期,所以我设法将问题范围缩小到那里(除非我在这里吠叫完全错误的树)

这里出了什么问题,如何解决?

我觉得答案很简单,但我只是没有看到。这是结果页面的代码(它是根据我在互联网上找到的多个教程拼凑而成的,但我确实了解代码中发生的情况),并且我添加了 var_dump($_SESSION) 命令来查看传入的变量该 session 将在问题解决后被删除。

<?php
include('db.php'); // include your code to connect to DB.
session_start();
var_dump($_SESSION);
if (mysql_real_escape_string($_POST['regbutton']) == submit||(!isset($_SESSION['submit1']))||!(isset($_SESSION['submit2']))){
$_SESSION['search']=mysql_real_escape_string($_POST['regbutton']);
$_SESSION['submit1']=mysql_real_escape_string($_POST['place']);
$_SESSION['submit2']=mysql_real_escape_string("empty");
$place =mysql_real_escape_string($_SESSION['submit1']);
$clicked=mysql_real_escape_string($_SESSION['search']);
var_dump($_SESSION);
}
elseif(mysql_real_escape_string($_POST['magbutton']) == submit|| (!isset($_SESSION['submit1']))||!(isset($_SESSION['submit2']))){
$_SESSION['search']=mysql_real_escape_string($_POST['magbutton']);
$_SESSION['submit1']=mysql_real_escape_string($_POST['mag1']);
$_SESSION['submit2']=mysql_real_escape_string($_POST['mag2']);
$mag1 = mysql_real_escape_string($_SESSION['submit1']);
$mag2 = mysql_real_escape_string($_SESSION['submit2']);
$clicked=mysql_real_escape_string($_SESSION['search']);
var_dump($_SESSION);
}
else{
var_dump($_SESSION);
echo "No records found. Session might be broken.";
exit;
}
$tbl_name="quake"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$result = mysql_query($query);
$total_pages = mysql_fetch_array($result);
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "resultz.php"; //your file name (the name of this file)
$limit = 30; //how many items to show per page
$page = $_GET['page'];
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

/* Get data. */
if (mysql_real_escape_string($_POST['regbutton']) == submit){
$query = "SELECT * FROM quake WHERE region LIKE '%of%, $place%' LIMIT $start, $limit";
}
elseif (mysql_real_escape_string($_POST['magbutton']) == submit){
if ($mag2 >= $mag1) {
$query = "SELECT * FROM quake WHERE magnitude BETWEEN '$mag1' and '$mag2' LIMIT $start, $limit";
}
else{
$query = "SELECT * FROM quake WHERE magnitude BETWEEN '$mag2' and '$mag1' LIMIT $start, $limit";
}
}
else{
echo "No records found.";
exit;
}

$result = mysql_query($query) or die(mysql_error());;

/* 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.= "...";
$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.= "...";
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.= "...";
$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.= "...";
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";
}

?>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<link rel="stylesheet" media="print" type="text/css" href="print.css" />
<title>Recent Earthquakes</title>
</head>
<div id="header">
QUAKE SEARCH
</div>
<div id="header2">
Search the latest quakes
</div>
<table border=1>
<thead>
<td>Source</td>
<td>EqID</td>
<td>Version</td>
<td>Date/Time</td>
<td>Latitude</td>
<td>Longitude</td>
<td>Magnitude</td>
<td>Depth</td>
<td>NST</td>
<td>Region</td>
</thead>
<?php
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
?>
<?=$pagination?>

这是搜索表单:

<?php
session_start();
include ('db.php');
?>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<link rel="stylesheet" media="print" type="text/css" href="print.css" />
<title>Recent Earthquakes</title>
</head>
<div id="header">
QUAKE SEARCH
</div>
<div id="header2">
Search the latest quakes
</div>
<div id="search">
<form name="magsearch" action="resultz.php" method="post">
<label>Search by Magnitude:</label>
<select name="mag1">
<option>1.0</option>
<option>2.0</option>
<option>3.0</option>
<option>4.0</option>
<option>5.0</option>
<option>6.0</option>
<option>7.0</option>
<option>8.0</option>
<option>9.0</option>
<option>10.0</option>
</select>
<select name="mag2">
<option value=1>1.0</option>
<option value=2>2.0</option>
<option value=3>3.0</option>
<option value=4>4.0</option>
<option value=5>5.0</option>
<option value=6>6.0</option>
<option value=7>7.0</option>
<option value=8>8.0</option>
<option value=9>9.0</option>
<option>10.0</option>
</select>
<input name="magbutton" type="submit" value="submit" />
<input type="hidden" name="content" value="search">
</form>
<form name ="regsearch" action="resultz.php" method="post">
<label>Search by Region:</label>
<input name="place" type="text" size="14" />
<input name="regbutton" type="submit" value="submit" />
<input type="hidden" name="content" value="search">
</form>
</div>

最佳答案

重点是什么

if (mysql_real_escape_string($_POST['regbutton']) == submit||

如果您不在 SQL 操作中使用表单数据,则无需转义它 - 您只需在此处使用 PHP 进行比较,站点中没有针对此特定代码行的数据库。此外,您还将转义表单值与未定义的常量进行比较 - 请注意 submit 周围缺少引号。 PHP 会礼貌地将其视为不带引号的字符串,但会发出警告 - 鉴于您没有提到收到任何警告,您可能正在关闭 display_errors - 这意味着您不会从代码中看到任何问题报告。

这些错误在您的代码中重复出现,因此不要只修复这一行 - 修复整个脚本。

将该行更改为

if ($_POST['regbuttn'] == 'submit') || etc...

然后去阅读 PHP 手册,了解如何启用 display_errors 。关闭此功能后,您将在黑暗中工作并反复搬起石头砸自己的脚。

另外...不要使用表单字段的存在来确定是否发生了 POST。这是不可靠的。使用

if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... }

相反,这是 100% 可靠的。

关于php - PHP/MySQL 搜索引擎的 session 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11527083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com