2017-05-28 127 views
0

我的评论有问题。我可以将它们插入我的朋友制作的数据库中,并在正确的页面中回显它们,但删除部分不起作用。 拥有帐户的人可以删除自己的评论,管理员可以删除任何评论。但是当我点击评论的删除按钮时,我什么都不做,当我再次点击它删除该页面中的每一条评论时,有人可以帮忙吗?当我点击删除按钮时,我只想删除该特定评论,而不是全部。此外,数据库中的密钥是评论发布的日期。评论删除按钮不起作用

这里的comments.php

<!DOCTYPE html> 
<html> 
<link rel="stylesheet" type="text/css" href="/cssfolder/comments.css"> 
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans%22%3E"> 
<head> 
<title>Page Title</title> 
</head> 
<body> 
<div class="comment"> 
<form method="post" action=""> 
<textarea name='message' class="area" id='message' placeholder="Leave a comment"></textarea><br/> 
<br> 
<input type="submit" class="commentbutton" name="comment" value="Comment"> 
<br> 
</form> 
</div> 
<div class="commentcontainer"> 
<?php 
    date_default_timezone_set('America/Curacao'); 
    $db = new PDO('mysql:host=localhost;dbname=id1552202_accounts', 'id1552202_thecouch', 'Fargo123'); 
    $url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
$link = parse_url($url)['path']; 
    $path = ltrim($link, '/'); 
    try { 
      $zoekfilm = $db->prepare("SELECT film_id FROM Reviews WHERE path = :path"); 
      $zoekfilm->bindParam("path", $path); 
      $zoekfilm->execute(); 
      $film = $zoekfilm->fetch(); 
      } catch(PDOException $b){ 
      die("Error!: " . $b->getMessage()); 
      } 
    $hoeveel = $db->prepare("SELECT * FROM comments WHERE film_id = :id "); 
    $hoeveel->bindParam("id", $film[0]); 
    $hoeveel->execute(); 
    $count = $hoeveel->rowCount(); 
    echo "<br><b>" . $count . " Comments</b><br><br>"; 
    if(isset($_POST['comment'])){ 
     if(empty($_POST['message'])){ 
      echo "There's no message"; 
      echo "<br>"; 
      echo "<br>"; 
     } else { 
     if(isset($_SESSION['loggeduser'])){ 
     $message = $_POST['message']; 
     $datum = date('YmdHis'); 
     $username = $_SESSION['loggeduser'][0];  
    $nospam = $db->prepare(" SELECT comment FROM comments WHERE comment = :message AND film_id = :id"); 
      $nospam->bindParam("message", $message); 
      $nospam->bindParam("id", $film[0]); 
      $nospam->execute(); 
      if($nospam->rowCount() === 1){ 
       echo "No spam please"; 
      } else { 
      try{ 
      $addcomment = $db->prepare("INSERT INTO comments(Usernames, film_id, comment, date) VALUES (:username, :id , :comment, :datum)"); 
      $addcomment->bindParam("username", $username); 
      $addcomment->bindParam("id", $film[0]); 
      $addcomment->bindParam("comment", $message); 
      $addcomment->bindParam("datum", $datum); 
       $addcomment->execute(); 
      } catch(PDOException $c){ 
      die("Error!: " . $c->getMessage()); 
      } 
      } 
     } else { 
     header("Location: /signin.php"); 
    } 
    } 
    } 
    try { 
    $showcomments = $db->prepare("SELECT * FROM comments WHERE film_id = :id ORDER BY date DESC"); 
     $showcomments->bindParam("id", $film[0]); 
    $showcomments->execute(); 

     while($result = $showcomments->fetch(PDO::FETCH_ASSOC)){ 
      if(isset($_SESSION['admin'])){ 
      echo '<div class="commentdiv">'; 
    echo '<p><b>'.$result['Usernames'].'</b></p>'; 
    echo '<p class="tijd"><i><small>'. $result['date'] .'</small></i></p>'; 
    echo '<p> '.$result['comment'].'</p>'; 
       echo '<br>'; 
      echo '<form method="post" action="">'; 
      echo '<input type="submit" value="Delete Comment" name="delete" class="commentbutton" style="width:200px;">'; 
      echo $result['date']; 
      echo '<br>'; 
      echo '</form>'; 
        $delete = $result['date']; 
          if(isset($_POST['delete'])){ 
        $verwijderen = $db->prepare(" DELETE FROM comments WHERE comments.date = :datum LIMIT 1"); 
        $verwijderen->bindParam("datum", $delete); 
            $verwijderen->execute(); 
           } 
echo '</div>'; 
     } else if(isset($_SESSION['loggeduser'][0])) { 
         echo '<div class="commentdiv">'; 
    echo '<p><b>'.$result['Usernames'].'</b></p>'; 
    echo '<p class="tijd"><i><small>'. $result['date'] .'</small></i></p>'; 
    echo '<p> '.$result['comment'].'</p>'; 
     echo '<br>'; 
      echo '<form method="post" action="">'; 
      echo '<input type="submit" value="Delete Comment" name="delete" class="commentbutton" style="width:200px;">'; 

      echo '<br>'; 
      echo '</form>'; 
     echo '</div>'; 
         $delete = $result['date']; 
            if(isset($_POST['delete'])){ 
        $verwijderen = $db->prepare(" DELETE FROM comments WHERE comments.date = :datum "); 
        $verwijderen->bindParam("datum", $delete); 
            $verwijderen->execute(); 
           } 
     } else { 
         echo '<div class="commentdiv">'; 
    echo '<p><b>'.$result['Usernames'].'</b></p>'; 
    echo '<p class="tijd"><i><small>'. $result['date'] .'</small></i></p>'; 
    echo '<p> '.$result['comment'].'</p>'; 
echo '</div>'; 
     } 
     } 
} catch(PDOException $a){ 
      die("Error!: " . $a->getMessage()); 
    } 
    ?> 
    </div> 
</body> 
</html> 
+0

该程序是错误的。将删除查询及其执行置于循环之外。 – JazZ

+0

我在哪里放删除查询呢?它没有任何东西,现在 –

回答

0

查询删除页面的所有评论,因为这是在while循环,你不给一个唯一的ID,以确保您从数据库中删除的权利评论。所以只要页面有评论删除给定日期的所有评论,查询就会重复。

的解决办法是:

  • 添加主键到comments表,如果它没有一个呢,
  • 主键的值添加到value属性的删除按钮,
  • 把删除查询后while循环,
  • 使用主键从删除按钮取到删除的权利评论,
  • 修复您的代码缩进(最重要)。

的代码应该是这样的:

// ... 
echo '<button type="submit" value="'.$result['id_comment'].'" name="delete" class="commentbutton" style="width:200px;">'.$result['date'].'</button>'; 
// Then outside of the loop : 
if (isset($_POST['delete']) && !empty['delete']) { 
    $verwijderen = $db->prepare("DELETE FROM comments WHERE id_comment = :id_comment"); 
    $verwijderen->bindParam("id_comment", $_POST['delete']); // note that the $_POST['delete'] value is now the id of the comment. 
    $verwijderen->execute(); 
} 

这必须给你的想法。祝你好运。 ; )

+0

没问题,但我在我的数据库中的关键是发表评论的日期,所以而不是$ result ['id_comment']我把$ result ['date']?它会以同样的方式工作吗? –

+1

什么是日期格式?最好使用唯一的ID。为什么不向表中添加自动增量主键? – JazZ

+0

这是一个学校项目,如果我有时间做,我会这样做,我很欣赏答案,但我真的没有很多时间。那么,有什么办法,我仍然可以做到这一点,但与日期,而不是一个ID? –