【问题标题】:Allow users to delete their own comment PHP允许用户删除自己的评论 PHP
【发布时间】:2014-01-09 12:41:00
【问题描述】:

我想允许我的用户通过链接从数据库中删除他们自己的 cmets。但是我遇到的问题是我的评论没有删除,也没有收到任何错误消息。

这是我“salonpage.php”中“deletepost.php”页面的链接:

if ($row['userID'] == $_SESSION['userID']){
$str_comments .=" <p><a href='deletepost.php?pID=".$row['ID']."&salonid=$salonid'>Delete comment</a>";

这是我的“deletepost.php”页面中的代码:

<?php
  require_once("checklog.php");
  require_once("nifunctions.php");

  $postID = trim($_GET['pID']);

  if ($postID != '' && is_numeric($postID))
  {
      if (!$db_server)
       {
        die("unable to connect to database: ".mysqli_connect_error($db_server));
       }
       else
       {
         $_GET['salonid'] and ($_GET['salonid'] != '');
         $salonid = clean_string($db_server, $_GET['salonid']);

         mysqli_select_db($db_server, $db_database) or die ("couldnt find database");

         //Delete post from comments
         $query= "DELETE FROM comments WHERE ID=$postID";
         mysqli_query($db_server,$query) or 
         die("comment delete failed" . mysqli_error($db_server));

         //redirect back to index page
         header("Location: salonpage.php?salonid=$salonid");

       }
       //db close
       mysqli_close($db_server);
   }
?>

【问题讨论】:

  • 您遇到的错误是什么?
  • 如果您没有收到任何错误,请尝试包含以下代码行:ini_set('display_errors', 1); error_reporting(E_ALL & ~E_NOTICE);
  • 我认为它在这条线上打破了:$_GET['salonid'] and ($_GET['salonid'] != '');。那应该是什么?
  • 我有一个“Déjà Vue”
  • 你可以通过 echo $query 进行调试;看看你得到了什么。

标签: php mysqli comments sql-delete


【解决方案1】:

通过更改我的查询来解决问题:

//Print out existing comment
$query = "SELECT * FROM comments JOIN users ON comments.userID = users.ID WHERE salonID=$salonid";

到:

//Print out existing comment
$query = "SELECT comments.*, users.Username FROM comments JOIN users ON comments.userID = users.ID WHERE salonID=$salonid"; 

感谢您的帮助!

【讨论】:

    【解决方案2】:

    检查$_GET['pID'] 值以确保它已设置。

    否则,您的查询将不会被执行。您的第一个 if 将返回 false,您将在最后关闭您的连接。

    如果没有这个 if,查询将被正确执行,但不会找到要删除的任何寄存器。

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2011-10-15
      • 2013-11-03
      • 1970-01-01
      相关资源
      最近更新 更多