2016-06-13 100 views
1

所以我有一個數據庫程序運行良好的連接和所有除了一件事我試圖讓我的數據庫中定義的「forum_name」並將其設置爲我的標題。很多時候,如果我點擊鏈接,它會將我帶到該帖子或論壇頁面,它將獲得forum_name下的內容並將其作爲標題打印出來。使用PHP和MySql數據庫設置網頁標題

這是db_connect.php

<?php 
$db = new mysqli("localhost", "root", "","forum") or die("ERROR: With Connecetion"); 
?> 

這是復位:

<?php 
session_start(); 
require"db_connect.php"; 
//get the page id 
if(isset($_GET['id']) &&is_numeric($_GET['id'])){ 
    $id = $_GET['id']; 
}else{ 
    die("Error! Does not exist!"); 
} 
//check if Valid Id 
$idCheck = $db->query("SELECT * FROM forum_tabl WHERE forum_id = 'id'"); 
if($idCheck->num_rows !==0){ 
    die("error"); 
} 
$row = $idCheck->fetch_object(); 
$sql = "SELECT post_id, post_title FROM forum_post WHERE forum_id? AND type= 'o'"; 
if($query = $db->prepare($sql)){ 
    $query->bind_params('s', $id); 
    $query->bind_result($post_id, $post_title); 
    $query->execute(); 
    $query->store_result(); 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title><?= $row->forum_name?></title> 
</head> 

<body> 

<div id="container"> 
    <!-- Theres content that will go here --> 
</div> 

</body> 
</html> 
+0

'<?='快捷鍵在某些服務器上不起作用。試試'<?php echo $ row-> forum_name; ?>' – developerwjk

+0

不行仍然不起作用。 @developerwjk我知道快捷方式在我的服務器上工作,但雅我收到錯誤通知:試圖獲得標題線上的非對象的屬性。 –

+0

標題應該來自第一個還是第二個查詢?在第一個查詢中,你有'id ='id''這可能是不正確的,因爲id可能應該是一個數字,對吧? – developerwjk

回答

0

我認爲這個問題是在這條線。

$idCheck = $db->query("SELECT * FROM forum_tabl WHERE forum_id = 'id'"); 

您必須將其替換如下。

$idCheck = $db->query("SELECT * FROM forum_tabl WHERE forum_id = '$id'"); 
+0

謝謝,但我也必須改變if語句 –

+0

是的。在這種情況下,您必須更改if語句$ idCheck-> num_rows == 0而不是$ idCheck-> num_rows!== 0 –

+0

這應該是'WHERE forum_id =?'不是這個。 'bind_param'調用是正確的,可以在這裏重複以解決問題。 – tadman

0
<?php 
session_start(); 
require"db_connect.php"; 
//get the page id 
if(isset($_GET['id']) &&is_numeric($_GET['id'])){ 
    $id = $_GET['id']; 
}else{ 
    die("Error! Does not exist!"); 
} 
//check if Valid Id 
$idCheck = $db->query("SELECT * FROM forum_tabl WHERE forum_id = " . $id); 
if($idCheck->num_rows !==0){ 
    die("error"); 
} 
$row = $idCheck->fetch_object(); 
$sql = "SELECT post_id, post_title FROM forum_post WHERE forum_id? AND type= 'o'"; 
if($query = $db->prepare($sql)){ 
    $query->bind_params('s', $id); 
    $query->bind_result($post_id, $post_title); 
    $query->execute(); 
    $query->store_result(); 
} 
?> 

的「$ ID);是你在找什麼來得到這個工作,你可能想通過和雙重檢查一切如。‘forum_tabl’,並確保一切正確如果你還沒有這樣做的話。

還仔細檢查了以下額外的?可能搞亂東西,以及根據您的數據庫設置。

$sql = "SELECT post_id, post_title FROM forum_post WHERE forum_id? AND type= 'o'"; 

好運!