我對此有點麻煩。我有一個帶有各種鏈接的書籍的搜索結果頁面,所有鏈接到同一頁面(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'])){
[email protected]$_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>£" . $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];
} ?>
但無論我選擇什麼書環節,第一個搜索結果中的BOOKTITLE while循環被回顯出來。任何想法爲什麼?
任何幫助將不勝感激。
Hello @ user2197354:我會很高興,如果我可以幫助你,但只要你能夠張貼足夠的代碼,所以我可以站出來解決你的真正問題,因爲你的代碼是不夠的,至少你的程序的想法或邏輯不夠清晰我請你能給我額外的解釋和你的問題的額外信息,然後當你發佈你的代碼嘗試組織它的方式,將反映它的真實用途:) 我等待你的編輯,所以我可以幫助你:) –
謝謝你最後的呼吸。我希望我已經解釋過這一點現在好了:) – user2197354