我的网站有一个很好的搜索功能 - 只是现在我希望搜索结果显示在新页面上(而不是同一页面)。为此,我使用了全局变量$_SESSION
(使数组在新位置可用)和header("Location:
重定向到专用页面以获得结果。
运行搜索时,会生成以下错误:
这是我第一次使用$ _SESSION并且不知道如何解决这些错误 非常感谢能够理解什么是错误的。
以下是页面:
的的index.php
<?php require_once ('setup.php');
require_once ('function.php');
?>
<!DOCTYPE html>
<html>
<body>
<?php
if ( isset( $_GET['s'] ) ) {
$search_results='';
$search_term = htmlspecialchars($_GET['s'], ENT_QUOTES);
$search_results = search($conn, $search_term);
}
?>
<form role="search" method="get" class="search-form" action="">
<label>
<input type="search" class="search-field" placeholder="Search" value="searchitem" name="s">
</label>
<input type="submit" class="search-submit" value="Search">
</form>
<?php if (isset($search_results)) :
$_SESSION['search_output'] = $search_results;
header("Location: final.php");
exit;
endif; ?>
<body>
</html>
function.php
<?php
function search($conn, $search_term) {
if (session_id() === "") { session_start(); }
$sanitized = $conn->real_escape_string($search_term);
$query = $conn->query("
SELECT * FROM news WHERE title LIKE '%{$sanitized}%' OR body LIKE '%{$sanitized}%' OR sources LIKE '%{$sanitized}%' OR date LIKE '%{$sanitized}%'");
if ( ! $query->num_rows ) {
return false;
}
while( $row = $query->fetch_object() ) {
$rows[] = $row;
}
$search_results = array(
'count' => $query->num_rows,
'results' => $rows,
);
return $search_results;
}
?>
final.php
<?php session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
foreach($_SESSION['search_output'] as $value){
echo $value['results']->title;
}
?>
<body>
</html>
setup.php
<?php
$hn = 'localhost';
$db = 'boh';
$un = 'water';
$pw = 'water';
#DB Connection:
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
?>
答案 0 :(得分:1)
你没有在setup.php中使用session_start(),也没有使用function.php
你应该在使用$ _SESSION变量之前开始sesstion,使用下面的代码来启动会话,
if (session_id() === "") { session_start(); }
答案 1 :(得分:1)
注意:未定义的索引:第12行的final.php中的search_output
您需要在顶级 index.php 文件中使用session_start();
。所以开始你的会话,你将解决未定义的索引问题。
警告:在final.php中为foreach()提供的参数无效 12
对于第二个问题,您可以这样使用:
<?php
if(isset($_SESSION['search_output']) && is_array($_SESSION['search_output']))
{
foreach($_SESSION['search_output'] as $value)
{
echo $value['results']->title;
}
}
?>
您还需要在function.php文件中进行一些修改:
function search($conn, $search_term) {
$sanitized = $conn->real_escape_string($search_term);
$query = $conn->query("
SELECT * FROM news WHERE
title (LIKE '%{$sanitized}%' OR body LIKE '%{$sanitized}%'
OR sources LIKE '%{$sanitized}%'
OR date LIKE '%{$sanitized}%'"));
if ( ! $query->num_rows ) {
return false;
}
$rows = array(); // ADD THIS
while( $row = $query->fetch_object() ) {
$rows[] = $row;
}
$search_results = array(
'count' => $query->num_rows,
'results' => $rows,
);
return $search_results;
}
更新1:
在与OP讨论之后,你需要修改你的final.php文件,因为你在$_SESSION['search_output']
的结果索引中有另一个循环,所以最终的实现应该是:
<?php
if(isset($_SESSION['search_output']) && is_array($_SESSION['search_output']))
{
foreach($_SESSION['search_output']['results'] as $value){
echo $value->title;
}
}
?>