2013-12-16 40 views
0

只是試圖在URL上傳遞變量,以便在回顯時我可以單擊它並根據數據庫記錄打開它自己的內容。現在這個顯示數據庫中的所有記錄,但是我想要做的是傳遞一個URL,這樣每個博客ID都會有自己的URL,當點擊它時將打開單個條目而不是所有條目。變量URL PHP和MySQL

編輯現在我可以顯示具有ID的條目行,其中'ID'最後包含URL變量。我是否需要創建另一個查詢以在我的迷你博客上回復個人條目?

<? 

$db = // connection to db and authentication to connecting to db; 

#$postID = $_GET['postID']; // I'm thinking to use a $_GET global variable to work with URL variable 
$command = "select * from $table_name"; // I'm thinking to add the Id here or something or create another query to echo the linked URL 'viewblog.php?postID=$data->blogID' 
$result = $db->query($command); 

while ($data = $result->fetch_object()) { 
echo "<TR><TD><a href='viewblog.php?postID=$data->blogID'>".$data->blogID."</a></TD>"; 
echo "<TD>".$data->author."</TD>"; 
echo "<TD>".$data->date."</TD>"; 
echo "<TD>".$data->entry."</TD></TR>\n"; 
} 
$result->free(); 
$db->close; 
+0

@佛瑞德-II-這樣做相同的結果,而($數據= $ result-> fetch_row()){ 回聲'.$row['blogID'].''; echo「」。$ data ['author']。「」; echo「」。$ data ['date']。「
」; echo「」。$ data ['entry']。「 \ n」; } – mythoslife

+0

是的,我想過之後,這就是爲什麼我刪除了我的評論。 –

+0

@ Fred-ii- oh ok – mythoslife

回答

0
<?php 
//$db connect to database 

// Entry form sanitation of $_POST 

// Insert PHP file to MySQL 

// View all blog posts 

$postID = $_GET['postID']; // I guess I should sanitize this as well 
if (!empty($postID)) { 
    $command = "select * from $table_name where blogID = $postID"; 
    $result = $db->query($command); 
    while ($data = $result->fetch_object()) { 
    $postID = $data->blogID; 
    echo "<TR><TD>".$postID."</TD>"; 
    echo "<TD>".$data->author."</TD>"; 
    echo "<TD>".$data->date."</TD>"; 
    echo "<TD>".$data->entry."</TD></TR>\n"; 
    } 
    $result->free(); 
} 
else { 
$command = "select * from $table_name"; 
$result = $db->query($command); 
while ($data = $result->fetch_object()) { 
    $postID = $data->blogID; 
    echo "<TR><TD><a href='viewblog.php?postID=$postID'>".$postID."</a></TD>"; 
    echo "<TD>".$data->author."</TD>"; 
    echo "<TD>".$data->date."</TD>"; 
    echo "<TD>".$data->entry."</TD></TR>\n"; 
} 
$result->free(); 
} 


$db->close; 

?> 
0

您正在選擇表格中的所有條目。使用以下命令:

$db = // connection to db and authentication to connecting to db; 

$postID = $_GET['postID']; // ?? 
$db->real_escape_string(trim($postID)); 
$command = "select * from $table_name WHERE `postID`=$postID"; 
$result = $db->query($command); 
// Ensure results before outputting 
if ($result->num_rows) while($data = $result->fetch_assoc()){ 
    $data['blogID'] = $postID; 
    echo "<TR><TD><a href='viewblog.php?postID='>".$data['blogID']."</a> </TD>"; //?? 
    echo "<TD>".$data['author']."</TD>"; 
    echo "<TD>".$data['date']."<BR></TD>"; 
    echo "<TD>".$data['entry']."</TD></TR>\n"; 
} else echo "No entry found!"; 
$result->free(); 
$db->close; 
+1

容易受到SQL注入的影響 – Mike

+0

請不要教這樣的初學者不安全的代碼。 **這是我們必須告訴人們必須更改所有代碼的原因,因爲他們的代碼不安全,**這是大多數人的原因*哦,我會做的以後*並且永遠不會接近它。這比它的好壞更具破壞性(因爲當他們想進行另一次SQL調用時,它們會複製粘貼這個**代碼)。 – h2ooooooo

+0

注意。我通過'mysql_real_escape_string()'函數更新了我的代碼以防止不安全因素。 –

0

您可以創建這樣一些功能,基於URL

function single_blog($Post_id){ 
$sql = "SELECT * FROM your_table WHERE post_id = ? LIMIT 1"; 
$stmt = $this->db->prepare($sql); 
$stmt->execute(array($Post_id); 
return $stmt->fetch(); 
} 
+0

嗯@穆罕默德不知道我現在明白這個功能。 – mythoslife

3

爲什麼這個腳本是給所有條目返回一個職位?

因爲正在發送到數據庫的最終查詢是一樣的東西

select * from TABLE_NAME 

將使用星號返回,因爲您正在所有條目*選擇

你問了之後就可以如果執行的最終查詢包含「blogID」,則在獲取結果並開始獲取結果之前獲取。

http://www.w3schools.com/sql/sql_where.asp

您還應該使用所獲取的或交的ID在呼應結果(這樣可以在點擊的時候,每個博客都有自己的ID中的鏈接)。

這可能是這樣的

$postID = $_GET['postID']; 

//Add filtering by id to select statement 
$command = "select * from '$table_name' obj WHERE obj.blogID = '$postID'"; 

$result = $db->query($command); 
while($data = $result->fetch_assoc()){ 
$data['blogID'] = $postID; 

//Add ID to echoed link 
echo "<TR><TD><a href='viewblog.php?postID='".$data['blogID']."> Some Blog (ID: ".$data['blogID'].") </a> </TD>"; 
echo "<TD>".$data['author']."</TD>"; 
echo "<TD>".$data['date']."</TD>"; 
echo "<TD>".$data['entry']."</TD></TR>\n"; 
} 

注意有關該代碼的安全問題。你應該使用更安全的方式來做到這一點。我只是解釋結果。

至於自動遞增,它可以在您第一次創建表時啓動。這是爲了在數據庫中插入新行時。當您使用自動遞增功能時,您不必手動輸入ID。

http://www.w3schools.com/sql/sql_autoincrement.asp

通知:HTML BR元不應裏面結構中使用。

希望它有幫助。

+0

謝謝我試過了,這個只是顯示URL上的變量,但現在我想回顯它。 '#$ postID = $ _GET ['postID']; ??? $ command =「select * from $ table_name」; //添加更多? $ result = $ db-> query($ command); while($ data = $ result-> fetch_object()){ print「".$data->blogID."」; print「」。$ data-> author。「」; print「」。$ data-> date。「
」; print「」。$ data-> entry。「 \ n」;; } $ result-> free(); $ db-> close;'有什麼想法? – mythoslife

+0

我編輯@Braza ...我仍然試圖做到這一點,以便將ID所鏈接的回顯行打開到一個新窗口,以迴應它的個人內容/條目。 – mythoslife

+0

這應該通過在您的ANCHOR中添加一個TARGET屬性來完成,值爲'_blank',例如: TEXT Braza