Php 注释删除按钮不工作
我对我的评论有意见。我可以将它们插入到我朋友创建的数据库中,并在正确的页面中回显它们,但删除部分不起作用。 拥有帐户的人可以删除自己的评论,管理员可以删除任何评论。但是当我点击一条评论的删除按钮时,我什么也不做,当我再次点击时,它会删除该页面中的所有评论,有人能帮我吗?当我点击一个删除按钮时,我只想删除那个特定的评论,而不是全部。此外,数据库中的键是评论发布的日期 下面是comments.phpPhp 注释删除按钮不工作,php,mysql,pdo,Php,Mysql,Pdo,我对我的评论有意见。我可以将它们插入到我朋友创建的数据库中,并在正确的页面中回显它们,但删除部分不起作用。 拥有帐户的人可以删除自己的评论,管理员可以删除任何评论。但是当我点击一条评论的删除按钮时,我什么也不做,当我再次点击时,它会删除该页面中的所有评论,有人能帮我吗?当我点击一个删除按钮时,我只想删除那个特定的评论,而不是全部。此外,数据库中的键是评论发布的日期 下面是comments.php <!DOCTYPE html> <html> <link rel="s
<!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>
页面标题
查询将删除页面的所有注释,因为它位于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();
}
/。。。
回显“.$result['date']”;
//然后在循环之外:
如果(isset($_POST['delete'])&&!empty['delete']){
$verwijderen=$db->prepare(“从注释中删除,其中id_comment=:id_comment”);
$verwijderen->bindParam(“id_comment”,$_POST['delete']);//请注意,$_POST['delete']值现在是注释的id。
$verwijderen->execute();
}
这一定会让你产生想法。祝你好运 程序是错误的。将删除查询及其执行置于循环之外。那么,我将删除查询置于何处?现在什么都不知道,但我数据库中的关键是评论发布的日期,所以我应该把$result['id_comment']改为$result['date']?它会以同样的方式工作吗?日期格式是什么?最好使用一个唯一的ID。为什么不在表中添加一个自动递增主键呢?这是一个学校项目,如果我有时间这样做,我会这样做,我很感谢你的回答,但我真的没有太多时间。那么,除了用日期而不是id之外,我还有什么办法可以做到这一点吗?@luisalcantara在现有表中添加主键真的很容易,也很快。然后,使用上面的答案,它将按预期工作:altertable comments ADD COLUMN id\u comment INT(10)UNSIGNED PRIMARY KEY AUTO\u INCREMENT代码>@luisalcantara您尝试过这个解决方案吗?如果使用日期,问题是:如果同一日期有多个条目,查询将删除哪个条目?换句话说,它可能会删除错误的评论。