2014-11-03 116 views
0

我在頁面上有多個鏈接,其中每個鏈接都假設從數據庫返回特定行數據。當鏈接被點擊時,用戶被轉發到顯示與該鏈接相關的信息的另一個頁面。這裏是代碼:從數據庫返回特定行

//db connection: (using xampp) 
mysql_connect('localhost', 'root', ''); 
mysql_select_db('db_name'); 
$sql = "SELECT * FROM user_input"; 
$records = mysql_query($sql); 

//code: 
<div> 
$open_report = mtsql_fetch_assoc($records); 
    echo "Error Report# {$open_report['id']}; 
    echo "<p>" .$open_report['comments'] . "</p>"; 
</div> 

問題是它總是返回同一行數據。數據庫中的每一行都與一個鏈接相關聯,並且當單擊該鏈接時,我想返回數據庫中相關聯的數據行。我認爲這可能與這一行有關:$ sql =「SELECT * FROM user_input」;但我不知道如何解決它。如果任何人都可以幫助它將不勝感激。

+0

剛剛看到一個錯字:應該是:$ open_report = mysql_fetch_assoc($ records);回聲「錯誤報告#{$ open_report ['id']}」; – 2014-11-03 17:28:47

+0

目前,您正在從'user_input'表中選擇所有(*)列。你不包括'where'條件來指定哪些行。您的鏈接應該包含它們指向的行的唯一標識符,以便您可以將它們包含在SQL查詢中並挑選出特定的行。 – danhardman 2014-11-03 17:32:04

+0

那麼當行依賴於用戶點擊的鏈接時,我該如何指定一行呢? – 2014-11-03 17:35:20

回答

0

我調整了我的答案,使它更好地流動。我也注意到你正在使用mysql_而不是mysqli_。由於mysql已折舊,因此您需要使用mysqli_。

編輯:這將是顯示所有錯誤報告的頁面。您需要以超鏈接的形式輸出它們,將超參數傳遞給顯示詳細信息的頁面。

$sql = "SELECT ID, Description, etc, etc from reports"; 
$open_reports = mysqli_query($sql); 

//error check here as well if ANY results were returned 
while($row = mysqli_fetch_array($open_reports, MYSQLI_ASSOC)) { 
    echo '<a href="detailspage.php?id=' . $open_reports['ID'] . '">'' . $open_reports['Description'] . '</a>'; 
} 

這會給你看起來像

detailspage.php?id=1
detailspage.php?id=2
等鏈接...

在 「detailspage.php」 你可以捕捉ID和顯示器上的動態信息同一頁。

if (isset($_GET['ID'])){ 
    $sql = "Select * from user_input where ID='" . $_GET['id'] . "'"; 
    $records = mysqli_query($sql) 

    while($open_report = mysqli_fetch_array($records, MYSQLI_ASSOC)) { 
     echo "Error Report# " . $open_report['id'] . "<br/>"; 
     echo "<p>" .$open_report['comments'] . "</p>"; 
    } 
} 
+0

我已經使用循環在一個頁面上顯示數據庫中的一些信息。在那個頁面上,我已經應用了一個鏈接,指向從數據庫返回的所有錯誤報告。當單擊錯誤報告鏈接時,我想在另一個頁面上顯示與該錯誤報告相關的信息。如果我爲每個鏈接分配一個ID,我將能夠在另一個頁面上引用該ID並使用您建議的查詢? – 2014-11-03 17:52:09

+0

是的,讓我編輯並添加更多信息 – 2014-11-03 17:56:25

+0

好的。我可以在mysql查詢中包含$變量嗎? – 2014-11-03 17:57:36