php - 使用PHP从数据库中删除评论

标签 php mysql sql-delete

我的网站是管理员登录网站,因此用户无法注册。不过,他们可以在网站上发表评论。我在评论上有删除按钮,但没有意识到任何人都可以删除任何人的评论。我如何更改此设置,以便当管理员登录时,他们能够删除不适当的评论(如果有)并从公众中删除删除按钮。

这是我的 Comment.php 代码,其中包含删除按钮功能:

   <?php 

   session_start();
   require_once 'templates/open.php';
   require_once 'connect.php';
   require_once 'functions/cleanstring.php';
   require_once 'functions/encrypt.php';

   ?>

另一个代码文件:

   <?php
   $db_hostname = 'localhost';
   $db_database = 'cs12e2g_MyFirstDB'; //'Your database name'
   $db_username = 'cs12e2g_DBuser'; //'your username';
   $db_password = 'vtjppqs7'; //'Your password';
   $db_status = 'not initialised';
   $db_server = mysqli_connect($db_hostname, $db_username, $db_password);
   $db_status = "connected";
   if (!$db_server){
die("Unable to connect to MySQL: " . mysqli_connect_error());
$db_status = "not connected";
   }


    // Includes and variables always required

    require_once 'recaptcha/recaptchalib.php';
    require_once 'functions/cleanstring.php';
    $privatekey = "6Lem4-gSAAAAADsaa9KXlzSAhLs8Ztp83Lt-x1kn";
    $publickey = "6Lem4-gSAAAAAMHLAVbieIknMtUZo71ZKzzCkoFN";
    mysqli_select_db($db_server, $db_database);

    $str_message = "";
    if (!$db_server){
        die("Unable to connect to MySQL: " . mysqli_connect_error());
    }else{

        if(isset($_GET['delete'])){
            $deleteq="DELETE FROM comments WHERE ID={$_GET['delete']} LIMIT 1";

            $deleter=mysqli_query($db_server, $deleteq);
            IF($deleter){
                echo"<p>That message was deleted!</p>";}}




        //Test whether form has been submitted 
        if(trim($_POST['submit']) == "Submit"){
            //Handle submission
            $resp = recaptcha_check_answer ($privatekey,
                                            $_SERVER["REMOTE_ADDR"],
                                            $_POST["recaptcha_challenge_field"],
                                            $_POST["recaptcha_response_field"]);
            if (!$resp->is_valid) {
                // What happens when the CAPTCHA was entered incorrectly
                $str_message = "The reCAPTCHA wasn't entered correctly. Go back and try it          
    again. 
                                (reCAPTCHA said: " . $resp->error . ")";
            } else {

                // Your code here to handle a successful verification
               $comment = $_POST['comment'];
                if($comment != ""){
                    $query = "INSERT INTO comments (comment) VALUES ('$comment')";
                    mysqli_query($db_server, $query) or die("Comment insert failed: " .        
    mysqli_error($db_server) );
                    $str_message = "Thanks for your comment!";
                }else{
                    $str_message = "Invalid form submission";
                }
            }
        }
        //Create page with or without submission 
        $query = "SELECT * FROM comments";
        $result = mysqli_query($db_server, $query);
        if (!$result) die("Database access failed: " . mysqli_error($db_server) );
        while($row = mysqli_fetch_array($result)){ 
        $ID= $row['ID'];
            $str_result .=  "<p><em>Comment $j (" . $row['commDate'] . 
                    ")</em><br /> " .$row['comment'] . "</p>
                    <a href ='commentnow.php?delete=$ID
                    '>Delete</a><hr />"; 
        }
        mysqli_free_result($result);
    } 

     ?>




    <h1>What do you think?</h1>


   <p><h5>Did you find everything you wanted? Please comment below:<h5></p>

    <form action="commentnow.php" method="post">
        <textarea rows="10" cols="50" name="comment"></textarea><br />
        <?php echo recaptcha_get_html($publickey); ?>
        <input type="submit" name="submit" value="Submit" /> 
    </form> 
    <span style="color:#FF0000;">
    <?php echo $str_message; ?></span>
    <hr /> 
    <h2>Comments:</h2>
    <?php echo $str_result; ?> 




    </div>

     <?php
require_once 'templates/close.php';


?>

我有一个members.php页面,对应于管理员登录的时间(他们是唯一可以访问此页面的人),删除按钮代码是否必须放在这里?那么他们是唯一可以使用该功能的人吗?如果是的话,它会去哪里,如何去?

最佳答案

限制删除按钮仅对管理员显示。此外,这还意味着您可以以某种方式识别登录用户是否是管理员。

if ($is_admin) {
// Code to display button
}

同时在后台检查登录用户是否为admin

if ($is_admin) {
    // Code to delete comment
    delete_comment();
}

关于php - 使用PHP从数据库中删除评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033069/

相关文章:

php - PDO:fetchAll 分配数组变量

mysql - 如何使用vb将Combobox和checklistbox保存到mysql php中

MySQL 删除 "inactive"行

php - 如何将子域重写为 URL 中的变量?

php - 如何使用 PHP 动态发布 ical 文件以供 Google 日历读取?

php - 选择选项后不显示数据

php - 如何在 Laravel 5.2 中更新用户表

mysql - 删除除最后三个记录之外的记录

SQL选择直接删除

php - drupal 7 中未定义的偏移错误