2017-05-28 158 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? –