【发布时间】:2018-05-08 23:16:58
【问题描述】:
我知道如何从一个页面到另一个页面获取搜索结果,但我现在所处的场景是我有一个搜索表单 (searchform.php),用户在其中将搜索条件输入到一个或多个字段中,然后单击提交按钮和结果显示在 search.php 页面上的表格中。现在我需要做的是将这些查询结果发送到另一个页面(list.php),这是一个格式化为报告的页面。我尝试过使用 $_SESSION、$_GET、foreach 等。我还尝试将我的代码与我网站和互联网上其他页面上的代码进行比较,但无济于事。似乎没有一个适合我的情况。现在我正在使用 $_SESSION 并且只在表的每一列中获取单词“Array”并且只获取一行数据。当我确实从查询中获取实际数据时,它只带来了查询的最后一个结果。例如,search.php 显示了 6 个结果,但 list.php 页面只显示了最后一个结果。我在这上面花了几天时间。有人会帮助我吗? 这是我的 search.php 页面上的部分代码(它有效 - 虽然我不确定我是否需要中间的 $_SESSION 信息):
if !empty($_POST['id'])||!empty($_POST['title'])||!empty($_POST['numavail'])||!empty($_POST['categoryname'])||!empty($_POST['genretype'])){
$id = mysqli_real_escape_string($dbc, $_POST['id']);
$title = mysqli_real_escape_string($dbc, $_POST['title']);
$numavail = mysqli_real_escape_string($dbc, $_POST['numavail']);
$categoryname = mysqli_real_escape_string($dbc, $_POST['categoryname']);
$genretype = mysqli_real_escape_string($dbc, $_POST['genretype']);
$query="SELECT * FROM dvd WHERE (title LIKE '%$title%')
AND (numavail LIKE '%$numavail%')
AND (categoryname LIKE '%$categoryname%')
AND (genretype LIKE '%$genretype%')
ORDER BY title ASC";
}else {
$query="SELECT * FROM dvd ORDER BY title ASC";
}
$result = @mysqli_query ($dbc, $query);
$num = mysqli_num_rows($result);
if ($num > 0) { // If it ran OK, display all the records.
echo "<h4>Your search returned $num records.</h4><p></p>";
//Table header:
echo '<table align="center" width="950px" cellspacing="0" border=".5px" ! important><tr>
<th>Title</th><th>Qty</th><th>Category</th><th>Genre</th></tr>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
//Fetch all the records...
session_start();
$_SESSION['id']=['id'];
$_SESSION['title']=['title'];
$_SESSION['numavail']=['numavail'];
$_SESSION['categoryname']=['categoryname'];
$_SESSION['genretype']=['genretype'];
echo '<td align="left" width="20%" height="25px">'.$row['title'].'</td>
<td align="right" width="5%">'.$row['numavail'].'</td>
<td align="left" width="20%">'.$row['categoryname'].'</a></td>
<td align="left" width="30%">'.$row['genretype'].'</a></td>
<td align="center" width="5%"><a href=../dvd/updateform.php?id='.$row['id'].'><em>Update</em></a></td>
<td align="center" width="5%"><a href=../dvd/deleteconfirm.php?id='.$row['id'].'><em>Delete</em></a></td>
</tr>
';
}
// End of While statement
echo "</table>";
这是我的 list.php 页面上的部分代码(不工作):
session_start();
include ('../../includes/reportheader.php'); // Include the header file.
echo ('<h1>DVD SEARCH RESULTS</h1>');
//Table displaying records.
'<div id="rpttable">';
echo '<table align="center" width="750px" cellspacing="0" border=".5px" ! important><tr>
<th>Movie Title</th><th>Qty</th><th>Category</th><th>Genre</th></tr>';
echo '<td align="left" width="20%" height="25px">'.$_SESSION['title'].'</td>
<td align="center" width="5%">'.$_SESSION['numavail'].'</td>
<td align="left" width="20%">'.$_SESSION['categoryname'].'</td>
<td align="left" width="30%">'.$_SESSION['genretype'].'</a></td>
</tr>
</div>
';
echo "</table>";
【问题讨论】:
-
您不能在
session_start();之前输出(html、空格、回声等)任何内容,否则会话将失败。将其放在文档的第一行。 (除非你有输出缓冲) -
知道了。谢谢你,蓝先生。我在我的代码中做了这个更改。