2014-11-22 76 views
0

我正在製作一個名單和鏈接到他們的完整信息列表。所以,我有一個簡單的搜索引擎,通過名稱或具體數字進行搜索。我使用$ _SESSION來獲取人員的ID。問題是,當有超過1個名字,並且移動到特定人員的頁面時,出現列表中最後一個人的頁面! 所以,搜索引擎的代碼是:如何製作鏈接列表?

if(isset($_POST['search'])){ 
    $searchq = $_POST['search']; 
    $searchq = preg_replace("#[^0-9_a-z A-Z]#i","",$searchq); 

    $query = mysql_query("SELECT * FROM contract WHERE name LIKE '%$searchq%' OR student_code LIKE '%$searchq%'") or die("could not search"); 

    $count = mysql_num_rows($query); 
    if($count == 0){ 
     $output = 'There was no such results!'; 
    } 
    else{ 
     while($row = mysql_fetch_array($query)){ 

      $name = $row['name']; 
      $student_code = $row['student_code']; 
      $_SESSION['users_id'] = $row['users_id']; 
    $output = '<table border ="1"><tr><td>'.$name.' '.$student_code.' 
      </td> 
      <td> 
      <form action="cont.php" method="post"> 
    <label>Look at the contract:</label> 
    <input type="submit" name="submit" value=">>"> 
</form> 
</td> 
</tr> 
</table><br \> 

,並在頁面文件中的另一個腳本:

$users_id = $_SESSION['users_id']; 
$result = mysql_query("SELECT * FROM contract WHERE users_id = $users_id"); 
while($myrow = mysql_fetch_array($result)){ 
$output1 = 

回答

0

我明白你的問題的方式是,你有兩頁。一個搜索頁面,另一個頁面顯示關於特定結果的「更多信息」。

你在搜索中基本上做的是這樣的: 假設你有三個結果得到Id 1,4,7。

這是發生了什麼事情在你的while循環發生

  1. 設置$名稱$ student_code和$ _SESSION [ 'user_ID的']($ _SESSION [ 'user_ID的']現在是1
  2. 製備第一結果
  3. 設置$ $名稱和student_code $ _SESSION [ 'USER_ID']($ _SESSION [ 'USER_ID']現在是4
  4. 準備第二結果
  5. 設置$ $名稱和student_code $ _SESSION [ 'user_ID的']($ _SESSION [ 'user_ID的' 現在是7
  6. 準備第三結果

正如你可以看到你總是覆蓋會話密鑰,因此只有最後一個將可用,當你到達「cont.php」頁面(我猜測其他代碼是?)

一個簡單的解決方案將燒烤id到表單並在請求中將其發送到cont.php頁面。事情是這樣的:

<form action="cont.php" method="post"> 
    <label>Look at the contract:</label> 
    <input type="submit" name="submit" value=">>"> 
    <input type="hidden" name="user_id" value="' . $row['users_id'] . '"> 
</form> 

然後在cont.php您只需更改此:

$users_id = $_SESSION['users_id']; 

這個

$users_id = $_POST['users_id']; 

希望幫助:)

+0

是的,現在有用!謝謝! – iacm 2014-11-22 14:52:52

+0

@iacm可愛!你明白了爲什麼?讓我知道如果你需要更深入的解釋:) – ILOABN 2014-11-22 15:51:00

+1

是的,我明白了,現在我甚至在項目的另一部分修復了更多的錯誤,都感謝你:D – iacm 2014-11-22 16:15:49