簡體   English   中英

如何僅顯示發布評論的用戶的刪除和編輯鏈接?

[英]How do I show the delete and edit links only to the user who has posted the comment?

如何顯示發布評論的用戶的刪除和編輯鏈接? 就像在Facebook中一樣,只允許發布評論的人編輯或刪除評論。 以下是我的“顯示評論”,“顯示刪除”和“編輯評論” PHP文件:

<?php
    include_once("includes/settings.php");
    connect();
    $result=mysql_query("SELECT * FROM comments ORDER BY id DESC");
    echo "<table width='80%' border=0>";
    echo "<tr bgcolor='#CCCCCC'>";
    echo "<td>Name</td>";
    echo "<td>Comments</td>";;
    echo "</tr>";

    while($res=mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>".$res['Name']."</td>";
        echo "<td>".$res['Comments']."</td>"; 
        echo "<td><a href=\"edit_comment.php?id=$res[id]\">Edit</a> | <a href=\"includes/delete.php?id=$res[id]\">Delete</a></td>";
    }
    echo "</table>";
?>

下面是edit.php

<?php
    error_reporting(0);
    include_once("settings.php");
    connect();
    if(isset($_POST['submit'])) {
        $id = $_POST['id'];
        $Comments=$_POST['Comments'];
        if(empty($Comments)) {
            echo "<font color='red'>Comments field is empty.</font><br/>";
        }
        else {  
            $result=mysql_query("UPDATE comments SET Comments='$Comments' WHERE id=$id");
            echo "Your comments has been edited you will be redirected to the members area page automatically or <a href='../index_ma.php'>click here to go back</a>";
            header('refresh: 3; url=../index_ma.php');
        }
    }
?>
<?php
    $id = $_GET['id'];
    $result=mysql_query("select * from comments where id='$id'");
    while($res=mysql_fetch_array($result))
    {
        $Comments = $res['Comments'];
    }
?>

下面是delete.php

<?php
    include_once("settings.php");
    connect();
    $id = $_GET['id'];
    $result=mysql_query("DELETE FROM comments where id=$id");
    echo "Your comments has been deleted you will be redirected to the members area page automatically or <a href='../index_ma.php'>click here to go back</a>";
    header('refresh: 3; url=../index_ma.php');
?>

這取決於您的數據庫架構。 我假設您有一列存儲用戶ID。 這樣,您將像這樣:

if ($CurrentUserId == $res['CommentatorId']) {
  echo "<td><a href=\"edit_comment.php?id=$res[id]\">Edit</a> | <a href=\"includes/delete.php?id=$res[id]\">Delete</a></td>";
}
else {
  echo "<td></td>";
}

您將使用以上代碼塊,而不是第一個代碼塊中的echo "<td><a href=...行。

這是您的塊的外觀:

<?php
    include_once("includes/settings.php");
    connect();
    $result=mysql_query("SELECT * FROM comments ORDER BY id DESC");
    echo "<table width='80%' border=0>";
    echo "<tr bgcolor='#CCCCCC'>";
    echo "<td>Name</td>";
    echo "<td>Comments</td>";;
    echo "</tr>";

    while($res=mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td>".$res['Name']."</td>";
        echo "<td>".$res['Comments']."</td>"; 
        if ($CurrentUserId == $res['CommentatorId']) {
            echo "<td><a href=\"edit_comment.php?id=$res[id]\">Edit</a> | <a href=\"includes/delete.php?id=$res[id]\">Delete</a></td>";
        }
        else {
            echo "<td></td>";
        }
    }
    echo "</table>";
?>

我不確定是否這樣做,但是在評論表中,您需要保存發布評論的用戶的ID,然后在edit.php中,您需要檢查登錄用戶的ID是否等於ID。試圖編輯注釋的人的姓名。如果是,則進行編輯;如果不是,則不允許他進行編輯。

在以下代碼中,我假設您將用戶ID在注釋表中另存為user_id。

$comment_id = intval($_GET['id']);
$result = mysql_query("SELECT user_id FROM Comments WHERE id = $comment_id");
$row = mysql_fetch_array($result);
if($row['user_id'] == $user_id) {
  // Edit the comment
} else {
  // Not permitted to edit the comment
}

我還注意到您仍在使用已被棄用的mysql,因此建議您開始使用mysqli,我還注意到您沒有對變量進行清理,這是非常錯誤的,並可能導致數據庫被注入。 另外,在edit.php中,您在鏈接中發送了ID,因此是$ _GET而不是我在代碼中編輯的$ _POST。

僅當您的應用程序上有用戶和登錄系統時,此功能才適用。 如果我們假設您的注釋表中的“名稱”字段是唯一的,並分配了寫注釋的用戶名(當然來自用戶表),那么在成功登錄期間,您必須在會話變量中設置此“名稱”值,然后在打印注釋時,您需要檢查該會話值和注釋的“名稱”值以打印出編輯和刪除鏈接。

注意:此答案是一種

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM