单击链接时将会话变量传递给新页面

时间:2013-03-28 15:24:38

标签: php mysqli

我遇到了一些麻烦。我有一个包含各种链接的书籍的搜索结果页面,所有链接到同一页面(book.php),我想设置一个会话变量以传递到新页面,该页面应显示有关特定结果链接的更多信息。选择。当我选择while循环中的链接时,我试图将变量$ _SESSION ['booktitle']传递给book.php。搜索结果页面的代码为:

<form id="rform" name="formsort" action=""  method="post">
<select name="sort" id="sort" onChange="document.forms['formsort'].submit()">
<option value="default">Sort Results</option>
<option value="lowhigh">Price: Low to High</option>
<option value="highlow">Price: High to Low</option>
<option value="az">Alphabetical Order: A-Z</option>
<option value="za">Alphabetical Order: Z-A</option>
</select>
<?php
if(isset($_POST['title']))
    {
        echo '<input type="hidden" name="title" value="'.$_POST['title'].'" />';
    }
if(isset($_POST['author']))
    {
        echo '<input type="hidden" name="author" value="'.$_POST['author'].'" />';
    }
if(isset($_POST['isbn']))
    {
        echo '<input type="hidden" name="isbn" value="'.$_POST['isbn'].'" />';
    }
if(isset($_POST['keyword']))
    {
        echo '<input type="hidden" name="keyword" value="'.$_POST['keyword'].'" />';
    }
?>
</form>
<?php
include "include.php";
session_start();
// Defining variables from home.php if set
$title = mysql_real_escape_string($_POST['title']);
$author = mysql_real_escape_string($_POST['author']);
$isbn = mysql_real_escape_string($_POST['isbn']);
$keyword = mysql_real_escape_string($_POST['keyword']);

if(isset($_POST['sort'])){
$sort=@$_POST['sort'];}
// Determine the WHEREs to use
$where = array();
if ( ! empty($title))
$where[] = "booktitle LIKE '%" . $title . "%'";
if ( ! empty($author))
$where[] = "author LIKE '%" . $author . "%'";
if ( ! empty($isbn))
$where[] = "isbn LIKE '%" . $isbn . "%'";
if ( ! empty($keyword))
$where[] = "description LIKE '%" . $keyword . "%'";

//Determine the ORDERs to use
$order = array();
if ((isset($sort)) && ($sort=='lowhigh'))
$order[] = "price ASC";
if ((isset($sort)) && ($sort=='highlow'))
$order[] = "price DESC";
if ((isset($sort)) && ($sort=='az'))
$order[] = "booktitle ASC";
if ((isset($sort)) && ($sort=='za'))
$order[] = "booktitle DESC";
// Build the query
$query = 'SELECT * FROM book';
if ( ! empty($where))
$query .= ' WHERE ' . implode(' AND ', $where);
if(! empty($sort))
$query .= ' ORDER BY ' . implode($order);
//Display results
$result = mysqli_query($con, $query) or die("Error in query $query: " . mysqli_error());
while ($row = mysqli_fetch_array($result)) {
  ?><a id="1" href="book.php" border="0"><img src="<?php echo $row[12]; ?>" width="112px" height="150px" /></a>
<a href="book.php?booktitle='<?php echo $_SESSION['booktitle']=$row[1];?>'"><b><?php echo  $row[1] . ", (" . $row[5] . ")";?></b></a><br><?php
echo $row[2];
echo "<br><div style='text-align:right'> <b>&pound;" . $row[9] . "</b></div>";
echo "<b>Book description:</b> " . substr($row[3],0,300) . "...<br /><hr>";
}
mysqli_close($con);             //closes the connection
?>

我在book.php中使用:

调用它
<?php
include "include.php";
session_start();
$book=$_SESSION['booktitle'];

$query = "SELECT * FROM book WHERE booktitle='$book'";
$result = mysqli_query($con, $query) or die("Error in query $query: " . mysqli_error());
while ($row = mysqli_fetch_array($result)) {
echo $row[1];
} ?>

但无论我选择什么书籍链接,while循环中第一个搜索结果的书签都会被回显。知道为什么吗?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

<强>替换

$book=$_SESSION['booktitle'];

。通过

$book=$_GET['booktitle'];

您可以使用booktitle方法从链接获取GET

而不是使用$row[1]将其替换为$row['booktitle']

答案 1 :(得分:0)

我希望我能为你提供最大的帮助。

首先,我想了解第二个块是book.php时第一个块所在的文件名是什么?

然后我想建议你使用代码顶部的session_start()来消除任何“标题警告”。

然后我希望你向我解释为什么你把这个指令放入你的代码

<?php echo $_SESSION['booktitle']=$row[1]?>

为什么你不把它写成:

<?php
$_SESSION['booktitle'] = $row[1];
echo $_SESSION['booktitle'];
?>

然后总是在回显一次所需的$_SESSION['variable'] 之后在回复之后为它做一个unset()直接看起来就像这样{{1} }

我认为您的新书签搜索在上述代码中没有存储在新会话中,旧搜索结果只是存储尝试应用这些修改并告诉我它是否已解决:)